Skip to content

Commit c70c292

Browse files
Updated code examples for paperback version
1 parent acf20f3 commit c70c292

14 files changed

+49
-41
lines changed

README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ You can learn more about the book here:
2020

2121
* [Gumroad](https://superfastpython.gumroad.com/l/pmpj)
2222
* [Amazon](https://www.amazon.com/dp/B0B6YSF8XQ)
23-
* [GoodReads](https://www.goodreads.com/book/show/61606103-python-multiprocessing-pool-jump-start)
23+
* [Goodreads](https://www.goodreads.com/book/show/61606103-python-multiprocessing-pool-jump-start)
2424

2525
### Book Blurb
2626

27-
> How much faster can your python code run (if it used all CPU cores)?
27+
> How much faster could your python code run (if it used all CPU cores)?
2828
>
2929
> The multiprocessing.Pool class provides easy-to-use process-based concurrency.
3030
>
@@ -42,7 +42,6 @@ You can learn more about the book here:
4242
>
4343
> Each lesson ends with an exercise for you to complete to confirm you understood the topic, a summary of what was learned, and links for further reading if you want to go deeper.
4444
>
45-
> Stop copy-pasting code from outdated blog posts.
46-
> Stop trying to parse messy StackOverflow answers.
45+
> Stop copy-pasting code from outdated StackOverflow answers.
4746
>
4847
> Learn Python concurrency correctly, step-by-step.

src/lesson01_pool.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# SuperFastPython.com
2-
# example of running a function in the multiprocessing pool
2+
# example running a function in the multiprocessing pool
33
from multiprocessing import Pool
44

55
# a task to execute in another process

src/lesson02_default.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# SuperFastPython.com
2-
# example of reporting the details of a default multiprocessing pool
2+
# example reporting the details of a default pool
33
from multiprocessing import Pool
44

55
# protect the entry point
66
if __name__ == '__main__':
7-
# create a multiprocessing pool with the default number of workers
7+
# create a multiprocessing pool
88
pool = Pool()
99
# report the status of the multiprocessing pool
1010
print(pool)

src/lesson02_initalizer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# SuperFastPython.com
2-
# example of initializing worker processes in the multiprocessing pool
2+
# example initializing worker processes in the pool
33
from time import sleep
44
from multiprocessing import Pool
55

src/lesson03_map.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
# a task to execute in another process
66
def task(arg):
77
# report a message
8-
print(f'This is another process with {arg}', flush=True)
8+
print(f'From another process {arg}', flush=True)
99
# return a value
1010
return arg * 2
1111

1212
# entry point for the program
1313
if __name__ == '__main__':
1414
# create the multiprocessing pool
1515
with Pool() as pool:
16-
# issue multiple tasks to the pool and process return values
16+
# issue multiple tasks and process return values
1717
for result in pool.map(task, range(10)):
1818
print(result)

src/lesson03_starmap.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
# SuperFastPython.com
2-
# example of executing multiple tasks with multiple arguments
2+
# example of multiple tasks with multiple arguments
33
from multiprocessing import Pool
44

55
# a task to execute in another process
66
def task(arg1, arg2, arg3):
77
# report a message
8-
print(f'This is another process with {arg1}, {arg2}, {arg3}', flush=True)
8+
print(f'From another process {arg1}, {arg2}, {arg3}',
9+
flush=True)
910
# return a value
1011
return arg1 + arg2 + arg3
1112

@@ -15,6 +16,6 @@ def task(arg1, arg2, arg3):
1516
with Pool() as pool:
1617
# prepare task arguments
1718
args = [(i, i*2, i*3) for i in range(10)]
18-
# issue multiple tasks to the pool and process return values
19+
# issue multiple tasks and process return values
1920
for result in pool.starmap(task, args):
2021
print(result)

src/lesson04_map_async.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# SuperFastPython.com
2-
# example of executing multiple task with Pool.map_async()
2+
# example executing multiple task with Pool.map_async()
33
from multiprocessing import Pool
44

55
# a task to execute in another process
66
def task(arg):
77
# report a message
8-
print(f'This is another process with {arg}', flush=True)
8+
print(f'From another process {arg}', flush=True)
99
# return a value
1010
return arg * 2
1111

@@ -15,6 +15,6 @@ def task(arg):
1515
with Pool() as pool:
1616
# issue multiple tasks to the pool
1717
async_result = pool.map_async(task, range(10))
18-
# process return values once all issued tasks have completed
18+
# process return values once all tasks completed
1919
for result in async_result.get():
2020
print(result)

src/lesson04_starmap_async.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
# SuperFastPython.com
2-
# example of executing multiple tasks with multiple arguments
2+
# example with multiple tasks with multiple arguments
33
from multiprocessing import Pool
44

55
# a task to execute in another process
66
def task(arg1, arg2, arg3):
77
# report a message
8-
print(f'This is another process with {arg1}, {arg2}, {arg3}', flush=True)
8+
print(f'From another process {arg1}, {arg2}, {arg3}',
9+
flush=True)
910
# return a value
1011
return arg1 + arg2 + arg3
1112

src/lesson05_imap.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ def task(arg):
99
# block for a random fraction of a second
1010
sleep(random())
1111
# report a message
12-
print(f'This is another process with {arg}', flush=True)
12+
print(f'From another process {arg}', flush=True)
1313
# return a value
1414
return arg * 2
1515

1616
# entry point for the program
1717
if __name__ == '__main__':
1818
# create the multiprocessing pool
1919
with Pool(4) as pool:
20-
# issue multiple tasks to the pool and process return values
20+
# issue multiple tasks and process return values
2121
for result in pool.imap(task, range(10)):
2222
print(result)

src/lesson05_imap_unordered.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# SuperFastPython.com
2-
# example of executing multiple task with Pool.imap_unordered()
2+
# example executing multiple task with imap_unordered()
33
from time import sleep
44
from random import random
55
from multiprocessing import Pool
@@ -9,14 +9,14 @@ def task(arg):
99
# block for a random fraction of a second
1010
sleep(random())
1111
# report a message
12-
print(f'This is another process with {arg}', flush=True)
12+
print(f'From another process {arg}', flush=True)
1313
# return a value
1414
return arg * 2
1515

1616
# entry point for the program
1717
if __name__ == '__main__':
1818
# create the multiprocessing pool
1919
with Pool(4) as pool:
20-
# issue multiple tasks to the pool and process return values
21-
for result in pool.imap_unordered(task, range(10)):
22-
print(result)
20+
# issue multiple tasks and process return values
21+
for rs in pool.imap_unordered(task, range(10)):
22+
print(rs)

src/lesson06_asyncresult.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# SuperFastPython.com
2-
# example of checking the status and handling the result of an async task
2+
# example check status and handle result of async task
33
from time import sleep
44
from random import random
55
from multiprocessing import Pool

src/lesson06_callback.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ def task(identifier):
1313
# generate a value
1414
value = random()
1515
# report a message
16-
print(f'Task {identifier} executing with {value}', flush=True)
16+
print(f'Task {identifier} executing with {value}',
17+
flush=True)
1718
# block for a moment
1819
sleep(value)
1920
# return the generated value
@@ -24,7 +25,8 @@ def task(identifier):
2425
# create and configure the multiprocessing pool
2526
with Pool() as pool:
2627
# issue tasks to the multiprocessing pool
27-
result = pool.apply_async(task, args=(0,), callback=result_callback)
28+
result = pool.apply_async(task, args=(0,),
29+
callback=result_callback)
2830
# close the multiprocessing pool
2931
pool.close()
3032
# wait for all tasks to complete

src/lesson07_check_prime_parallel.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# SuperFastPython.com
2-
# check if large numbers are prime concurrently with Pool.imap()
2+
# check if large numbers are prime concurrently
33
from math import sqrt
44
from math import floor
55
from multiprocessing import Pool
@@ -15,9 +15,9 @@ def is_prime(number):
1515
# check if the number divides by 2 with no remainder
1616
if number % 2 == 0:
1717
return False
18-
# limit on divisors we test, sqrt of n, +1 so range() will reach it
18+
# limit divisors to sqrt(n)+1 so range will reach it
1919
limit = floor(sqrt(number)) + 1
20-
# check for evenly divisible for odd numbers between 3 and sqrt(n)
20+
# check all odd numbers in range
2121
for i in range(3, limit, 2):
2222
# check if number is divisible with no remainder
2323
if number % i == 0:
@@ -40,9 +40,11 @@ def check_numbers_are_prime(numbers):
4040
# entry point
4141
if __name__ == '__main__':
4242
# define some numbers to check
43-
NUMS = [17977, 10619863, 106198, 6620830889, 80630964769, 228204732751,
44-
1171432692373, 1398341745571, 10963707205259, 15285151248481,
45-
99999199999, 304250263527209, 30425026352720, 10657331232548839,
46-
10657331232548830, 44560482149, 1746860020068409]
43+
NUMS = [17977, 10619863, 106198, 6620830889,
44+
80630964769, 228204732751, 1171432692373,
45+
1398341745571, 10963707205259, 15285151248481,
46+
99999199999, 304250263527209, 30425026352720,
47+
10657331232548839, 10657331232548830,
48+
44560482149, 1746860020068409]
4749
# check whether each number is a prime
4850
check_numbers_are_prime(NUMS)

src/lesson07_check_prime_sequential.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ def is_prime(number):
1414
# check if the number divides by 2 with no remainder
1515
if number % 2 == 0:
1616
return False
17-
# limit on divisors we test, sqrt of n, +1 so range() will reach it
17+
# limit divisors to sqrt(n)+1 so range will reach it
1818
limit = floor(sqrt(number)) + 1
19-
# check for evenly divisible for odd numbers between 3 and sqrt(n)
19+
# check all odd numbers in range
2020
for i in range(3, limit, 2):
2121
# check if number is divisible with no remainder
2222
if number % i == 0:
@@ -35,9 +35,12 @@ def check_numbers_are_prime(numbers):
3535
# entry point
3636
if __name__ == '__main__':
3737
# define some numbers to check
38-
NUMS = [17977, 10619863, 106198, 6620830889, 80630964769, 228204732751,
39-
1171432692373, 1398341745571, 10963707205259, 15285151248481,
40-
99999199999, 304250263527209, 30425026352720, 10657331232548839,
41-
10657331232548830, 44560482149, 1746860020068409]
38+
NUMS = [17977, 10619863, 106198, 6620830889,
39+
80630964769, 228204732751, 1171432692373,
40+
1398341745571, 10963707205259, 15285151248481,
41+
99999199999, 304250263527209, 30425026352720,
42+
10657331232548839, 10657331232548830,
43+
44560482149, 1746860020068409]
4244
# check whether each number is a prime
4345
check_numbers_are_prime(NUMS)
46+

0 commit comments

Comments
 (0)