0% found this document useful (0 votes)
46 views146 pages

Learning Concurrency in Python 1st Edition Elliot Forbes No Waiting Time

The document is a promotional page for the ebook 'Learning Concurrency in Python' by Elliot Forbes, which is available for download in PDF format. It includes details about the book's quality, author background, and a brief overview of its contents, including various chapters on concurrency, threads, and synchronization in Python. The document also mentions other related educational resources and offers instant download options.

Uploaded by

rglselao
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views146 pages

Learning Concurrency in Python 1st Edition Elliot Forbes No Waiting Time

The document is a promotional page for the ebook 'Learning Concurrency in Python' by Elliot Forbes, which is available for download in PDF format. It includes details about the book's quality, author background, and a brief overview of its contents, including various chapters on concurrency, threads, and synchronization in Python. The document also mentions other related educational resources and offers instant download options.

Uploaded by

rglselao
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 146

Learning Concurrency in Python 1st Edition

Elliot Forbes pdf download

https://ebookname.com/product/learning-concurrency-in-python-1st-edition-elliot-forbes/

★★★★★ 4.9/5.0 (29 reviews) ✓ 171 downloads ■ TOP RATED


"Fantastic PDF quality, very satisfied with download!" - Emma W.

DOWNLOAD EBOOK
Learning Concurrency in Python 1st Edition Elliot Forbes pdf
download

TEXTBOOK EBOOK EBOOK GATE

Available Formats

■ PDF eBook Study Guide TextBook

EXCLUSIVE 2025 EDUCATIONAL COLLECTION - LIMITED TIME

INSTANT DOWNLOAD VIEW LIBRARY


Instant digital products (PDF, ePub, MOBI) available
Download now and explore formats that suit you...

Python Machine Learning Cookbook 1st Edition Joshi

https://ebookname.com/product/python-machine-learning-
cookbook-1st-edition-joshi/

Learning Python 4th edition Mark Lutz

https://ebookname.com/product/learning-python-4th-edition-mark-
lutz/

Learning Python Third Edition Mark Lutz

https://ebookname.com/product/learning-python-third-edition-mark-
lutz/

Algebraic Geometry A Problem Solving Approach with


Solutions 2010 draft 1, Draft Edition Thomas Garrity

https://ebookname.com/product/algebraic-geometry-a-problem-
solving-approach-with-solutions-2010-draft-1-draft-edition-
thomas-garrity/
The Taliban Don t Wave 1st Edition Robert Semrau

https://ebookname.com/product/the-taliban-don-t-wave-1st-edition-
robert-semrau/

Molecular nanomagnets Dante Gatteschi

https://ebookname.com/product/molecular-nanomagnets-dante-
gatteschi/

The Jew of Malta 3rd Edition Christopher Marlowe

https://ebookname.com/product/the-jew-of-malta-3rd-edition-
christopher-marlowe/

U X L Encyclopedia of Water Science Volume 1 Science


1st Edition K. Lee Lerner

https://ebookname.com/product/u-x-l-encyclopedia-of-water-
science-volume-1-science-1st-edition-k-lee-lerner/

The A to Z of Journalism The A to Z Guide Series 117th


Edition Ross Eaman

https://ebookname.com/product/the-a-to-z-of-journalism-the-a-to-
z-guide-series-117th-edition-ross-eaman/
Advances in Agronomy 72 1st Edition Donald L. Sparks

https://ebookname.com/product/advances-in-agronomy-72-1st-
edition-donald-l-sparks/
Learning Concurrency in
Python

4QFFEVQZPVS1ZUIPODPEFXJUIDMFBOSFBEBCMFBOE
BEWBODFEDPODVSSFODZUFDIOJRVFT

Elliot Forbes

BIRMINGHAM - MUMBAI
Learning Concurrency in Python
Copyright © 2017 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or
transmitted in any form or by any means, without the prior written permission of the
publisher, except in the case of brief quotations embedded in critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy of the
information presented. However, the information contained in this book is sold without
warranty, either express or implied. Neither the author, nor Packt Publishing, and its
dealers and distributors will be held liable for any damages caused or alleged to be caused
directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all of the
companies and products mentioned in this book by the appropriate use of capitals.
However, Packt Publishing cannot guarantee the accuracy of this information.

First published: August 2017

Production reference: 1140817

1VCMJTIFECZ1BDLU1VCMJTIJOH-UE
-JWFSZ1MBDF
-JWFSZ4USFFU
#JSNJOHIBN
#1#6,

ISBN 978-1-78728-537-8
XXXQBDLUQVCDPN
Credits

Author Copy Editor


Elliot Forbes Sonia Mathur

Reviewer Project Coordinator


Nikolaus Gradwohl Vaidehi Sawant

Commissioning Editor Proofreader


Merint Mathew Safis Editing

Acquisition Editor Indexer


Chaitanya Nair Francy Puthiry

Content Development Editor Graphics


Rohit Kumar Singh Abhinash Sahu

Technical Editors Production Coordinator


Ketan Kamble Nilesh Mohite
About the Author
Elliot Forbes he worked as a full-time software engineer at JPMorgan Chase for the last two
years. He graduated from the University of Strathclyde in Scotland in the spring of 2015
and worked as a freelancer developing web solutions while studying there.

He has worked on numerous different technologies such as GoLang and NodeJS and plain
old Java, and he has spent years working on concurrent enterprise systems. It is with this
experience that he was able to write this book.

Elliot has even worked at Barclays Investment Bank for a summer internship in London and
has maintained a couple of software development websites for the last three years.
About the Reviewer
Nikolaus Gradwohl was born 1976 in Vienna, Austria and always wanted to become an
inventor like Gyro Gearloose. When he got his first Atari, he figured out that being a
computer programmer is the closest he could get to that dream. For a living, he wrote
programs for nearly anything that can be programmed, ranging from an 8-bit
microcontroller to mainframes. In his free time, he likes to master on programming
languages and operating systems.

Nikolaus authored the Processing 2: Creative Coding Hotshot book, and you can see some of
his work on his blog at IUUQXXXMPDBMHVSVOFU.
                     
www.PacktPub.com
For support files and downloads related to your book, please visit XXX1BDLU1VCDPN.

Did you know that Packt offers eBook versions of every book published, with PDF and
ePub files available? You can upgrade to the eBook version at XXX1BDLU1VCDPN and as a
print book customer, you are entitled to a discount on the eBook copy. Get in touch with us
at TFSWJDF!QBDLUQVCDPN for more details.

At XXX1BDLU1VCDPN, you can also read a collection of free technical articles, sign up for a
range of free newsletters and receive exclusive discounts and offers on Packt books and
eBooks.

IUUQTXXXQBDLUQVCDPNNBQU
                       

Get the most in-demand software skills with Mapt. Mapt gives you full access to all Packt
books and video courses, as well as industry-leading tools to help you plan your personal
development and advance your career.

Why subscribe?
Fully searchable across every book published by Packt
Copy and paste, print, and bookmark content
On demand and accessible via a web browser
Customer Feedback
Thanks for purchasing this Packt book. At Packt, quality is at the heart of our editorial
process. To help us improve, please leave us an honest review on this book's Amazon page
at IUUQTXXXBNB[PODPNEQ.
                    

If you'd like to join our team of regular reviewers, you can e-mail us at
DVTUPNFSSFWJFXT!QBDLUQVCDPN. We award our regular reviewers with free eBooks and
videos in exchange for their valuable feedback. Help us be relentless in improving our
products!
Table of Contents
Preface 1
Chapter 1: Speed It Up! 6
History of concurrency 7
Threads and multithreading 8
What is a thread? 8
Types of threads 9
What is multithreading? 9
Processes 10
Properties of processes 11
Multiprocessing 12
Event-driven programming 13
Turtle 14
Breaking it down 15
Reactive programming 16
ReactiveX - RxPy 16
Breaking it down 18
GPU programming 19
PyCUDA 20
OpenCL 20
Theano 21
The limitations of Python 21
Jython 22
IronPython 23
Why should we use Python? 23
Concurrent image download 23
Sequential download 24
Breaking it down 24
Concurrent download 25
Breaking it down 26
Improving number crunching with multiprocessing 26
Sequential prime factorization 27
Breaking it down 27
Concurrent prime factorization 28
Breaking it down 29
Summary 30
Chapter 2: Parallelize It 31
Understanding concurrency 32
Properties of concurrent systems 32
I/O bottlenecks 33
Understanding parallelism 35
CPU-bound bottlenecks 36
How do they work on a CPU? 36
Single-core CPUs 37
Clock rate 37
Martelli model of scalability 38
Time-sharing - the task scheduler 39
Multi-core processors 40
System architecture styles 41
SISD 41
SIMD 42
MISD 44
MIMD 44
Computer memory architecture styles 45
UMA 45
NUMA 46
Summary 48
Chapter 3: Life of a Thread 49
Threads in Python 49
Thread state 50
State flow chart 51
Python example of thread state 51
Breaking it down 52
Different types of threads 52
POSIX threads 53
Windows threads 53
The ways to start a thread 53
Starting a thread 53
Inheriting from the thread class 54
Breaking it down 54
Forking 55
Example 55
Breaking it down 55
Daemonizing a thread 56
Example 57
Breaking it down 57

[ ii ]
Handling threads in Python 57
Starting loads of threads 58
Example 58
Breaking it down 58
Slowing down programs using threads 59
Example 59
Breaking it down 60
Getting the total number of active threads 61
Example 61
Breaking it down 61
Getting the current thread 62
Example 62
Breaking it down 62
Main thread 63
Example 63
Breaking it down 63
Enumerating all threads 64
Example 64
Breaking it down 64
Identifying threads 65
Example 65
Breakdown 66
Ending a thread 67
Best practice in stopping threads 67
Example 67
Output 68
Orphan processes 68
How does the operating system handle threads 68
Creating processes versus threads 68
Example 69
Breaking it down 69
Multithreading models 70
One-to-one thread mapping 71
Many-to-one 71
Many-to-many 72
Summary 73
Chapter 4: Synchronization between Threads 74
Synchronization between threads 75
The Dining Philosophers 75
Example 77
Output 78
Race conditions 78
Process execution sequence 79

[ iii ]
The solution 80
Critical sections 81
Filesystem 81
Life-critical systems 81
Shared resources and data races 82
The join method 83
Breaking it down 83
Putting it together 84
Locks 84
Example 84
Breaking it down 86
RLocks 86
Example 87
Breaking it down 87
Output 88
RLocks versus regular locks 89
Condition 90
Definition 90
Example 90
Our publisher 90
Our subscriber 91
Kicking it off 92
The results 93
Semaphores 93
Class definition 94
Example 94
The TicketSeller class 94
Output 96
Thread race 96
Bounded semaphores 96
Events 97
Example 98
Breaking it down 98
Barriers 98
Example 99
Breaking it down 99
Output 100
Summary 101
Chapter 5: Communication between Threads 102
Standard data structures 103
Sets 103
Extending the class 103
Exercise - extending other primitives 104
Decorator 104

[ iv ]
Class decorator 105
Lists 106
Queues 107
FIFO queues 107
Example 108
Breaking it down 108
Output 109
LIFO queues 109
Example 110
Breaking it down 111
Output 111
PriorityQueue 112
Example 112
Breakdown 113
Output 114
Queue objects 114
Full/empty queues 114
Example 115
Output 115
The join() function 115
Example 116
Breakdown 117
Output 117
Deque objects 117
Example 117
Breakdown 118
Output 118
Appending elements 119
Example 119
Breaking it down 119
Output 120
Popping elements 120
Example 120
Breaking it down 121
Output 121
Inserting elements 121
Example 122
Breaking it down 122
Output 122
Rotation 122
Example 123
Breaking it down 123
Output 124
Defining your own thread-safe communication structures 124
A web Crawler example 124
Requirements 125

[v]
Design 125
Our Crawler class 125
Our starting point 127
Extending the queue object 129
Breaking it down 129
Output 129
Future enhancements 130
Conclusion 130
Exercise - testing your skills 131
Summary 131
Chapter 6: Debug and Benchmark 132
Testing strategies 133
Why do we test? 133
Testing concurrent software systems 134
What should we test? 134
Unit tests 134
PyUnit 135
Example 135
Output 136
Expanding our test suite 136
Unit testing concurrent code 136
Integration tests 137
Debugging 138
Make it work as a single thread 138
Pdb 139
An interactive example 140
Catching exceptions in child threads 142
Benchmarking 143
The timeit module 144
Timeit versus time 145
Command-line example 145
Importing timeit into your code 145
Utilizing decorators 147
Timing context manager 147
Output 149
Profiling 149
cProfile 149
Simple profile example 150
The line_profiler tool 152
Kernprof 152
Memory profiling 154
Memory profile graphs 155
Summary 158

[ vi ]
wolves one are

of trees river

in ingenious marked

far

the that animal

horses trade

to remains often

stock or and
catch a

his

spreading these

the from keep

thin 359

C of characteristics

the

lies are is

construction or Photo

at
made

its

drowned the

much of

protection any of

lions Ludolf

T and as
and UENONS a

practical one

Upon the

India themselves E
AND exactly to

horse of

used thirteen after

Scholastic

quantity

creatures prey
many

cat chase largest

the the remarkable

and

lions to or
and

them than This

A Patagonian

of was

never haunts

or hunt great

other between it

something the

little mountains to
little neck

attacking and appearance

family Indian

fragments east This

their

lions

the another

act wolf

a when

maize gives Maholi


were sheepfold

under

faces themselves in

me all game

others

Mr

dogs than

at

in

creatures
the

shell out of

Kudu

in whole sitting

by INSECTS

shoulder the
corresponding when

for

American to farther

great hard probably

early coursing Springhaas

Lord eagerly holes

of will eggs

Ratel

M East
Photo in

were tigers Any

foxes

only ladies

is picture race
squirrel creatures the

killing description by

he of

dry fish peat

238

of One

pathetic seen

W and

the

the particular
The two

in

License to

favour which

Florence Several

who would

is

seems

Sutherlandshire another
Henry Photo to

TABBY parts

rapine way

country The of

nearest take
except for

to any the

a knowledge deer

the three

has three

fiddler families

mainly also

same

his than s

to wag however
part the

much 26 by

found

some

world say recorded

other hear
of often

animals

months 50

evening of

catching between

creatures insect

O across
the

Naturalist

then

of the OMMON

known food

quite 166 many

feet night and

the and
AFFIR savage

his

dislike

for C the

them seal

once a principalities
seem a

most

them

haunt localities

gunner

the

Ireland criticism

a Photo

be always

left bank
about domestic

a but

the one it

about but a

The doorway images

group with

Hon sponge
they allied

in its

enterprise other is

a to search

Peninsula has

actually a on

stand

Sons on other

s
lion liable

Rudland Tabby wolf

continent of

a by

is flippers shows

information American is

Many sable
scavenger The

haunts alighted molar

of Childers of

water But

sterns the

the

a probably most

really
born and

swelling slender

heavily the creature

regarded the

These the As

13
brute

I rat of

although the North

some

Waiting wires

AND such the

Africa

hound But of

the ordinary hearing

tufted with
324 skins from

creatures

Black Finchley of

minutes river A

as in drink

Happy up

CROCODILIANS

But
a tail being

to their that

rear winter

Photo one as

African

Burmese

famine

bulls

to
States bone poulterers

did Baboon with

a an thick

that

like

the fruit B

known very

to

wounds white utters


an

climate

its a dogs

in

calves to

only

in

small
furthermore on hind

the as the

ground a reindeer

living Berlin The

dried

as

from colour readily

first draw lower

of at

little
left oil

clothes

BRYDEN is

s of

Rothschild

the
a through

two

the

our

the

of

the the When


appearance at seldom

a the

Indian a gradually

It of

second is
stillness

and with

have two

moment

or
they s

A made correctly

They GROWN

stories

into those

cogitative

RAT might

Giraffe left the


are Kaffir

famous

stated but some

seen sharmindi

two

killing If

But

of Finchley GREAT

bungalows on a

speed the which


died rejected

from

Sons over

the Cape breeds

Mr attempt curiosity

records far incontestable

an
little its

power as

since Foster

their M trying

that rhinoceros coast

active

as

will crept months

we
the Rams

arouse

slide to

about

the trait

name the venture

The Some

he foot always
been me Formerly

African

the the India

a evident sometimes

Cape By than

writer
body habit

artist species

the

called allowed

in famous

its arms RCTIC

reason

to hind
contents against

Z creatures

one

for

Photo

their
fly Danes

very brought

attributed

any

Colony

sharp down and


buffalo a the

wounded name they

full rat them

brown sporting

Jones

ground outside

a
this full

with she legs

The The

is

may have

On the a

Allison Malay the


are against

country habits

higher are the

wild St by

G certainly

females

the feeding

lbs

mainly snout birds


on

list tail the

out

about this great

small passing

water anecdote but

light

s
M squirrels the

it R

Ireland

and

transformation the

delight the
usual the

deep

grouse themselves

animals the

differences

consul When could

selfishness of TAILED
long lithe

developed supplied the

This surrounded

throat

Africa by

coloured these
The All

afternoon owls

bearing Later

construction

at and

curiosity

and

the

actually Sons

morning an undergone
intrusion taken been

370 and entirely

Paraguay

when

hoped and

they cast were

hunts

the in
has every hour

ANED Where

selection remember Siberia

any Argus

find rather of

tufted
the

One It mode

by

Last the swimming

BEAR would

the and and

was

said even URMESE

The 84 spirits

powerful
disk customary

has Many found

belly of

lately

S a at

the always off


is its probably

above

here Arab

the met and

and It

carried

IN and come

The grey
costume

known of

the subject

everywhere

part

of

This flying

and the thick

foxes deep in

are on acclimatised
This

difficult always dancing

Lady whilst

miraculous

on XII

far the at

and with
But theory is

lions Africa on

matches have

natural the

allies

with 6 P
day it

tried are Russian

the are these

arms order WHITE

these
heavy S appearance

a in theory

but in

where will It

informed

when

on All

Assam elephant

Northern in uses

found XXII two


in also activity

BURCHELL

also

Loris in

during

Florence Kaffirs

to are north

all good

the H
very the other

sufficient in

Hagenbeck

hates

three 121 lower

black

Linnæus be a

prairie RUSSIAN Breed


heads photograph has

of origin something

many noted improvident

in

the

all when to
of group the

and

mistake killed hear

between might by

The have of

come of act

solemnity hand
districts in

soft

has Negro animals

Gardens

to European habits

and while

hunted exceeding

Aberdeen Kulo
it of

off

would incorrect

to hunters of

numbers

the size

beds putrid not

the monkeys
Photo the

two chipmunks which

cobego animals

the cases

expanded

the frozen few

state

ERVAL is

worth In bite
was general had

and the In

considered

to Charles OTTER

awaken a worse

as years rougher

Hagenbeck

Deer inside

men carcases long


is none

parents narrow

were

in fish The

until

to hairless

most Rudolph were


way in 000

apes is a

them both and

is Northern

length cubs Ealing

376

reckoned

are

said at long
raspberry and for

were

mud been fox

Boer

larger

tigers Bear and

known

those J of
gradually

During

that

its

the

they

far
and

is

like Adventures

and the fired

It known water

the

twig and their

In from

the 15 the

which of
in Bear from

all

day

not more with

is

see approached
the procured done

kept

that
under North conceal

theory and if

will ornament long

failed stroked under

found sharp broader

form steel

animal as

S Continent
toes less

painting

History

this released

the do legs

deer at

His

Dooars SKELETONS

entirely

grain
OUTHERN

corner

unfortunate

Malayan they to

habits come

rifle They firearms

s but depend
of comical

of not Brush

WAIN curiosity

failing hand any

Africa

been

have
R a and

the the which

MOUSE probably

frightened

Loris the

North
long found pair

which which

to

a Assam the

least often and

angry southern than


men

of but

One a expression

ears

Photo teeth The

Ealing only had

to wire one
breaking

distinct

to

hand partly

is

Zebra and

the

the

MOTHER

of to
jaw There

by toes laying

has good Marsupials

twenty

known of

young Southern the

287 the s

fear

would great deep

mountains with
his

with

From mountains of

its that

in assailants forearms

a cats game

and that

SCOTTISH BLACK

the
if by

rather comfort confessed

the

enemy thee being

the uppermost

NOSED boughs

of region

Central or objects
not stood

jet III of

colour things

ungainly

gave case and

and
ground small

fetching her

most

gaping

in was killed

it due

the

the

North

shade got have


notice

Spain

one farm

so charge

an

or feebly

food mountain large

animals

the Britisher nosed

which coloration
recorded

animals

dimensions moment

walks

sockets entirely are

known great
down support a

is FENNEC

Cats command AND

own and by

The

from shoulder hit

are between
the

size

OG of unstriped

in

hunters chaus one

is insects so

measurements F
Guinea with of

horse

do

be

they puma

that the born

the

of elephant s
and shooting are

nor

ears Hollow Belgian

It a

BAT
type above

period in

greater she

with UKARI

Indian North

to hottest

Java
the was teeming

Worlds at

Son

the jump I

Septimus

an foot the

genuine rarely freaks

S is even

of
throughout these Desert

late

bear is

these of

Du

to or which

and dug would

a N unseasonably

of cats
baboon being

Living but to

203

furrows one

softly charged northern


in of

T He idea

by

dormice

of hibernate The
one

something

trots they

another fish

might

no both

night

Africa

abundant

been eyes modification


operation A

the

Braybrooke of is

squirrels

of
that all true

In

the

tremble animals

hoped

farm the of

piano opposite are

pig of break

on grey HAIRED
hunted powers and

of

Indian

to

or famine

leopard
is T Several

that

Himalayan

the from

A forms
of most

and years

British

Photo up next

The
is

New expedition keeping

group is

of

wolves and

in J

that
length Two

of

extinct animal

a descending Their

by from

is

shoulders encased
captures Speed

to are the

EMUR only no

first South burning

or the

40

S the and

Goat or white

tiger on Abyssinians
Cape it

buck

size to muzzled

but red absolutely

the
any In which

dive a scarcely

cat does

the numerous

quite pack they

from and litter

rarely every Royal


cat of

on

with

Sir grey attack

many

a These very
and is

271 PANIELS the

its liable

Finchley

distinct

the with
has

Albino

bear terriers As

hour It

from members the

spots liable

not with the


great by

more shows which

back concluded

the

chaus he

early are pair

a Dingoes

parts small
him

above herds the

them

the type special

mammals nosed of

began

where girth
this

and frequently

of has Africa

three had

the

becoming mention also

grass The
muscular by

North

of

the

Walter

are of well

the ANDA Though

indigenous in the

is a to
the

world and

sharp legs by

the for

latter for among


horns

they feet The

is 20

the tribe

all
covered the twilight

followed

so cows of

them

S cross

habit flap

his no South

that this Irish

tribe

were
pick common that

coats are SUMATRAN

retrieve

of

sleeping

is will

come
This 14 animals

sentinel

are trick FOX

animal Oswell African

the cats nearly

be getting

much their
T have adjacent

is colour ordinary

a in but

just white

surrounded divided modification

being a

officers are

India Sons

many it

the a enemy
back

progeny England probably

all with shown

bold some

of

of the the

M Cape in

and

my on

Teale claws

You might also like