Skip to content

Commit 0ca64d0

Browse files
Updated code examples for latest version of the book
1 parent c70c292 commit 0ca64d0

15 files changed

+46
-43
lines changed

src/lesson01_pool.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22
# example running a function in the multiprocessing pool
33
from multiprocessing import Pool
44

5-
# a task to execute in another process
5+
# custom function to be executed in a child process
66
def task():
7+
# report a message
78
print('This is another process', flush=True)
89

9-
# entry point for the program
10+
# protect the entry point
1011
if __name__ == '__main__':
1112
# create the multiprocessing pool
12-
pool = Pool()
13-
# issue the task
14-
async_result = pool.apply_async(task)
15-
# wait for the task to finish
16-
async_result.wait()
17-
# close the multiprocessing pool
18-
pool.close()
13+
with Pool() as pool:
14+
# issue the task
15+
async_result = pool.apply_async(task)
16+
# wait for the task to finish
17+
async_result.wait()
18+
# close the multiprocessing pool automatically

src/lesson01_process.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22
# example of running a function in a new process
33
from multiprocessing import Process
44

5-
# a task to execute in another process
5+
# custom function to be executed in a child process
66
def task():
7+
# report a message
78
print('This is another process', flush=True)
89

9-
# entry point for the program
10+
# protect the entry point
1011
if __name__ == '__main__':
1112
# define a task to run in a new process
1213
process = Process(target=task)
1314
# start the task in a new process
1415
process.start()
15-
# wait for the task to complete
16+
# wait for the child process to terminate
1617
process.join()

src/lesson02_initalizer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from time import sleep
44
from multiprocessing import Pool
55

6-
# task executed in a worker process
6+
# custom function to be executed in a child process
77
def task():
88
# report a message
99
print('Worker executing task...', flush=True)

src/lesson03_apply.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
# SuperFastPython.com
2-
# example of executing a task with Pool.apply()
2+
# example of executing a one-off task and waiting
33
from multiprocessing import Pool
44

5-
# a task to execute in another process
5+
# custom function to be executed in a child process
66
def task():
7+
# report a message
78
print('This is another process', flush=True)
89

9-
# entry point for the program
10+
# protect the entry point
1011
if __name__ == '__main__':
1112
# create the multiprocessing pool
1213
with Pool() as pool:

src/lesson03_map.py

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

5-
# a task to execute in another process
5+
# custom function to be executed in a child process
66
def task(arg):
77
# report a message
88
print(f'From another process {arg}', flush=True)
99
# return a value
1010
return arg * 2
1111

12-
# entry point for the program
12+
# protect the entry point
1313
if __name__ == '__main__':
1414
# create the multiprocessing pool
1515
with Pool() as pool:

src/lesson03_starmap.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
# example of multiple tasks with multiple arguments
33
from multiprocessing import Pool
44

5-
# a task to execute in another process
5+
# custom function to be executed in a child process
66
def task(arg1, arg2, arg3):
77
# report a message
88
print(f'From another process {arg1}, {arg2}, {arg3}',
99
flush=True)
1010
# return a value
1111
return arg1 + arg2 + arg3
1212

13-
# entry point for the program
13+
# protect the entry point
1414
if __name__ == '__main__':
1515
# create the multiprocessing pool
1616
with Pool() as pool:

src/lesson04_apply_async.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
# SuperFastPython.com
2-
# example of executing a task with Pool.apply_async()
2+
# example of executing a one-off task asynchronously
33
from multiprocessing import Pool
44

5-
# a task to execute in another process
5+
# custom function to be executed in a child process
66
def task():
7+
# report a message
78
print('This is another process', flush=True)
89

9-
# entry point for the program
10+
# protect the entry point
1011
if __name__ == '__main__':
1112
# create the multiprocessing pool
1213
with Pool() as pool:

src/lesson04_map_async.py

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

5-
# a task to execute in another process
5+
# custom function to be executed in a child process
66
def task(arg):
77
# report a message
88
print(f'From another process {arg}', flush=True)
99
# return a value
1010
return arg * 2
1111

12-
# entry point for the program
12+
# protect the entry point
1313
if __name__ == '__main__':
1414
# create the multiprocessing pool
1515
with Pool() as pool:

src/lesson04_starmap_async.py

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

5-
# a task to execute in another process
5+
# custom function to be executed in a child process
66
def task(arg1, arg2, arg3):
77
# report a message
88
print(f'From another process {arg1}, {arg2}, {arg3}',
99
flush=True)
1010
# return a value
1111
return arg1 + arg2 + arg3
1212

13-
# entry point for the program
13+
# protect the entry point
1414
if __name__ == '__main__':
1515
# create the multiprocessing pool
1616
with Pool() as pool:

src/lesson05_imap.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# SuperFastPython.com
2-
# example of executing multiple task with Pool.imap()
2+
# example of executing multiple tasks one-by-one
33
from time import sleep
44
from random import random
55
from multiprocessing import Pool
66

7-
# a task to execute in another process
7+
# custom function to be executed in a child process
88
def task(arg):
99
# block for a random fraction of a second
1010
sleep(random())
@@ -13,7 +13,7 @@ def task(arg):
1313
# return a value
1414
return arg * 2
1515

16-
# entry point for the program
16+
# protect the entry point
1717
if __name__ == '__main__':
1818
# create the multiprocessing pool
1919
with Pool(4) as pool:

src/lesson05_imap_unordered.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# SuperFastPython.com
2-
# example executing multiple task with imap_unordered()
2+
# example getting many task results in completion order
33
from time import sleep
44
from random import random
55
from multiprocessing import Pool
66

7-
# a task to execute in another process
7+
# custom function to be executed in a child process
88
def task(arg):
99
# block for a random fraction of a second
1010
sleep(random())
@@ -13,7 +13,7 @@ def task(arg):
1313
# return a value
1414
return arg * 2
1515

16-
# entry point for the program
16+
# protect the entry point
1717
if __name__ == '__main__':
1818
# create the multiprocessing pool
1919
with Pool(4) as pool:

src/lesson06_asyncresult.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from random import random
55
from multiprocessing import Pool
66

7-
# a task to execute in another process
7+
# custom function to be executed in a child process
88
def task():
99
# loop a few times to simulate a slow task
1010
for i in range(10):
@@ -15,7 +15,7 @@ def task():
1515
# report a message
1616
print(f'>{i} got {value}', flush=True)
1717

18-
# entry point for the program
18+
# protect the entry point
1919
if __name__ == '__main__':
2020
# create the multiprocessing pool
2121
with Pool() as pool:

src/lesson06_callback.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 a callback function for Pool.apply_async()
2+
# example of callback function for a one-off async task
33
from random import random
44
from time import sleep
55
from multiprocessing.pool import Pool
66

7-
# result callback function
7+
# custom function to be executed in a child process
88
def result_callback(return_value):
9+
# report a message
910
print(f'Callback got: {return_value}', flush=True)
1011

1112
# task executed in a worker process

src/lesson07_check_prime_parallel.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def check_numbers_are_prime(numbers):
3737
if isprime:
3838
print(f'{number} is prime')
3939

40-
# entry point
40+
# protect the entry point
4141
if __name__ == '__main__':
4242
# define some numbers to check
4343
NUMS = [17977, 10619863, 106198, 6620830889,

src/lesson07_check_prime_sequential.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def check_numbers_are_prime(numbers):
3232
if is_prime(number):
3333
print(f'{number} is prime')
3434

35-
# entry point
35+
# protect the entry point
3636
if __name__ == '__main__':
3737
# define some numbers to check
3838
NUMS = [17977, 10619863, 106198, 6620830889,
@@ -43,4 +43,3 @@ def check_numbers_are_prime(numbers):
4343
44560482149, 1746860020068409]
4444
# check whether each number is a prime
4545
check_numbers_are_prime(NUMS)
46-

0 commit comments

Comments
 (0)