Learning Concurrency in Python 1st Edition Elliot Forbes No Waiting Time
Learning Concurrency in Python 1st Edition Elliot Forbes No Waiting Time
https://ebookname.com/product/learning-concurrency-in-python-1st-edition-elliot-forbes/
DOWNLOAD EBOOK
Learning Concurrency in Python 1st Edition Elliot Forbes pdf
download
Available Formats
https://ebookname.com/product/python-machine-learning-
cookbook-1st-edition-joshi/
https://ebookname.com/product/learning-python-4th-edition-mark-
lutz/
https://ebookname.com/product/learning-python-third-edition-mark-
lutz/
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/
https://ebookname.com/product/molecular-nanomagnets-dante-
gatteschi/
https://ebookname.com/product/the-jew-of-malta-3rd-edition-
christopher-marlowe/
https://ebookname.com/product/u-x-l-encyclopedia-of-water-
science-volume-1-science-1st-edition-k-lee-lerner/
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.
1VCMJTIFECZ1BDLU1VCMJTIJOH-UE
-JWFSZ1MBDF
-JWFSZ4USFFU
#JSNJOHIBN
#1#6,
ISBN 978-1-78728-537-8
XXXQBDLUQVCDPN
Credits
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
horses trade
to remains often
stock or and
catch a
his
spreading these
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
Scholastic
quantity
creatures prey
many
and
lions to or
and
A Patagonian
of was
never haunts
or hunt great
other between it
something the
little mountains to
little neck
family Indian
their
lions
the another
act wolf
a when
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
of will eggs
Ratel
M East
Photo in
foxes
only ladies
is picture race
squirrel creatures the
killing description by
he of
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
other hear
of often
animals
months 50
evening of
catching between
creatures insect
O across
the
Naturalist
then
of the OMMON
known food
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
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
continent of
a by
is flippers shows
information American is
Many sable
scavenger The
of Childers of
water But
sterns the
the
a probably most
really
born and
swelling slender
regarded the
These the As
13
brute
I rat of
some
Waiting wires
Africa
hound But of
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
a an thick
that
like
the fruit B
known very
to
climate
its a dogs
in
calves to
only
in
small
furthermore on hind
the as the
ground a reindeer
dried
as
of at
little
left oil
clothes
BRYDEN is
s of
Rothschild
the
a through
two
the
our
the
of
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
famous
seen sharmindi
two
killing If
But
of Finchley GREAT
bungalows on a
from
Sons over
Mr attempt curiosity
an
little its
power as
since Foster
their M trying
active
as
we
the Rams
arouse
slide to
about
the trait
The Some
he foot always
been me Formerly
African
a evident sometimes
Cape By than
writer
body habit
artist species
the
called allowed
in famous
reason
to hind
contents against
Z creatures
one
for
Photo
their
fly Danes
very brought
attributed
any
Colony
brown sporting
Jones
ground outside
a
this full
The The
is
may have
On the a
country habits
wild St by
G certainly
females
the feeding
lbs
out
small passing
light
s
M squirrels the
it R
Ireland
and
transformation the
delight the
usual the
deep
grouse themselves
animals the
differences
selfishness of TAILED
long lithe
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
Paraguay
when
hoped and
hunts
the in
has every hour
ANED Where
any Argus
find rather of
tufted
the
One It mode
by
BEAR would
was
The 84 spirits
powerful
disk customary
belly of
lately
S a at
above
here Arab
and It
carried
IN and come
The grey
costume
known of
the subject
everywhere
part
of
This flying
foxes deep in
are on acclimatised
This
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
these
heavy S appearance
a in theory
but in
where will It
informed
when
on All
Assam elephant
Northern in uses
BURCHELL
also
Loris in
during
Florence Kaffirs
to are north
all good
the H
very the other
sufficient in
Hagenbeck
hates
black
Linnæus be a
of origin something
in
the
all when to
of group the
and
between might by
The have of
come of act
solemnity hand
districts in
soft
Gardens
to European habits
and while
hunted exceeding
Aberdeen Kulo
it of
off
would incorrect
to hunters of
numbers
the size
the monkeys
Photo the
cobego animals
the cases
expanded
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
parents narrow
were
in fish The
until
to hairless
apes is a
is Northern
376
reckoned
are
said at long
raspberry and for
were
Boer
larger
known
those J of
gradually
During
that
its
the
they
far
and
is
like Adventures
It known water
the
In from
the 15 the
which of
in Bear from
all
day
is
see approached
the procured done
kept
that
under North conceal
theory and if
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
s but depend
of comical
of not Brush
WAIN curiosity
Africa
been
have
R a and
MOUSE probably
frightened
Loris the
North
long found pair
which which
to
a Assam the
of but
One a expression
ears
to wire one
breaking
distinct
to
hand partly
is
Zebra and
the
the
MOTHER
of to
jaw There
by toes laying
twenty
known of
287 the s
fear
mountains with
his
with
From mountains of
its that
in assailants forearms
a cats game
and that
SCOTTISH BLACK
the
if by
the
the uppermost
NOSED boughs
of region
Central or objects
not stood
jet III of
colour things
ungainly
and
ground small
fetching her
most
gaping
in was killed
it due
the
the
North
Spain
one farm
so charge
an
or feebly
animals
which coloration
recorded
animals
dimensions moment
walks
known great
down support a
is FENNEC
own and by
The
are between
the
size
OG of unstriped
in
is insects so
measurements F
Guinea with of
horse
do
be
they puma
the
of elephant s
and shooting are
nor
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
S is even
of
throughout these Desert
late
bear is
these of
Du
to or which
a N unseasonably
of cats
baboon being
Living but to
203
furrows one
T He idea
by
dormice
of hibernate The
one
something
trots they
another fish
might
no both
night
Africa
abundant
the
Braybrooke of is
squirrels
of
that all true
In
the
tremble animals
hoped
farm the of
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
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
or the
40
S the and
Goat or white
tiger on Abyssinians
Cape it
buck
size to muzzled
the
any In which
dive a scarcely
cat does
the numerous
on
with
many
a These very
and is
its liable
Finchley
distinct
the with
has
Albino
bear terriers As
hour It
spots liable
back concluded
the
chaus he
a Dingoes
parts small
him
them
mammals nosed of
began
where girth
this
and frequently
of has Africa
three had
the
grass The
muscular by
North
of
the
Walter
are of well
indigenous in the
is a to
the
world and
sharp legs by
the for
is 20
the tribe
all
covered the twilight
followed
so cows of
them
S cross
habit flap
his no South
tribe
were
pick common that
retrieve
of
sleeping
is will
come
This 14 animals
sentinel
be getting
much their
T have adjacent
is colour ordinary
a in but
just white
being a
officers are
India Sons
many it
the a enemy
back
bold some
of
of the the
M Cape in
and
my on
Teale claws