Skip to content

Commit 98b9dbd

Browse files
first commit
0 parents  commit 98b9dbd

19 files changed

+428
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# git ignore
2+
**/.DS_Store
3+
.DS_Store

README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Python Multiprocessing Pool Jump-Start
2+
3+
![Python Multiprocessing Pool Jump-Start](cover.png)
4+
5+
* <https://github.com/SuperFastPython/PythonMultiprocessingPoolJumpStart>
6+
7+
This repository provides all source code for the book:
8+
9+
* *Python Multiprocessing Pool Jump-Start*: _Run Your Python Functions In Parallel With Just A Few Lines Of Code_, Jason Brownlee, 2022.
10+
11+
You can access all Python .py files directly here:
12+
13+
* [src/])(src/)
14+
15+
You can learn more about the book here:
16+
17+
* [GumRoad](https://superfastpython.gumroad.com/l/pmpj)
18+
* [GoodReads](https://www.goodreads.com/book/show/61606103-python-multiprocessing-pool-jump-start)
19+
20+
Blurb
21+
22+
How much faster can your python code run (if it used all CPU cores)?
23+
24+
The multiprocessing.Pool class provides easy-to-use process-based concurrency.
25+
26+
This is not some random third-party library, this is a class provided in the Python standard library (already installed on your system).
27+
28+
This is the class you need to use to make your code run faster.
29+
30+
There's just one problem. No one knows about it (or how to use it well).
31+
32+
Introducing: "Python Multiprocessing Pool Jump-Start". A new book designed to teach you multiprocessing pools in Python, super fast!
33+
34+
You will get a fast-paced, 7-part course to get you started and make you awesome at using the multiprocessing pool.
35+
36+
Each of the 7 lessons was carefully designed to teach one critical aspect of the multiprocessing pool, with explanations, code snippets and worked examples.
37+
38+
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.
39+
40+
Stop copy-pasting code from outdated blog posts.
41+
Stop trying to parse messy StackOverflow answers.
42+
43+
Learn Python concurrency correctly, step-by-step.

cover.png

758 KB
Loading

src/lesson01_pool.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# SuperFastPython.com
2+
# example of running a function in the multiprocessing pool
3+
from multiprocessing import Pool
4+
5+
# a task to execute in another process
6+
def task():
7+
print('This is another process', flush=True)
8+
9+
# entry point for the program
10+
if __name__ == '__main__':
11+
# 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()

src/lesson01_process.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# SuperFastPython.com
2+
# example of running a function in a new process
3+
from multiprocessing import Process
4+
5+
# a task to execute in another process
6+
def task():
7+
print('This is another process', flush=True)
8+
9+
# entry point for the program
10+
if __name__ == '__main__':
11+
# define a task to run in a new process
12+
process = Process(target=task)
13+
# start the task in a new process
14+
process.start()
15+
# wait for the task to complete
16+
process.join()

src/lesson02_default.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# SuperFastPython.com
2+
# example of reporting the details of a default multiprocessing pool
3+
from multiprocessing import Pool
4+
5+
# protect the entry point
6+
if __name__ == '__main__':
7+
# create a multiprocessing pool with the default number of workers
8+
pool = Pool()
9+
# report the status of the multiprocessing pool
10+
print(pool)
11+
# close the multiprocessing pool
12+
pool.close()

src/lesson02_initalizer.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# SuperFastPython.com
2+
# example of initializing worker processes in the multiprocessing pool
3+
from time import sleep
4+
from multiprocessing import Pool
5+
6+
# task executed in a worker process
7+
def task():
8+
# report a message
9+
print('Worker executing task...', flush=True)
10+
# block for a moment
11+
sleep(1)
12+
13+
# initialize a worker in the multiprocessing pool
14+
def initialize_worker():
15+
# report a message
16+
print('Initializing worker...', flush=True)
17+
18+
# protect the entry point
19+
if __name__ == '__main__':
20+
# create and configure the multiprocessing pool
21+
with Pool(2, initializer=initialize_worker) as pool:
22+
# issue tasks to the multiprocessing pool
23+
for _ in range(4):
24+
pool.apply_async(task)
25+
# close the multiprocessing pool
26+
pool.close()
27+
# wait for all tasks to complete
28+
pool.join()

src/lesson03_apply.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# SuperFastPython.com
2+
# example of executing a task with Pool.apply()
3+
from multiprocessing import Pool
4+
5+
# a task to execute in another process
6+
def task():
7+
print('This is another process', flush=True)
8+
9+
# entry point for the program
10+
if __name__ == '__main__':
11+
# create the multiprocessing pool
12+
with Pool() as pool:
13+
# issue a task and wait for it to complete
14+
pool.apply(task)

src/lesson03_map.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# SuperFastPython.com
2+
# example of executing multiple task with Pool.map()
3+
from multiprocessing import Pool
4+
5+
# a task to execute in another process
6+
def task(arg):
7+
# report a message
8+
print(f'This is another process with {arg}', flush=True)
9+
# return a value
10+
return arg * 2
11+
12+
# entry point for the program
13+
if __name__ == '__main__':
14+
# create the multiprocessing pool
15+
with Pool() as pool:
16+
# issue multiple tasks to the pool and process return values
17+
for result in pool.map(task, range(10)):
18+
print(result)

src/lesson03_starmap.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# SuperFastPython.com
2+
# example of executing multiple tasks with multiple arguments
3+
from multiprocessing import Pool
4+
5+
# a task to execute in another process
6+
def task(arg1, arg2, arg3):
7+
# report a message
8+
print(f'This is another process with {arg1}, {arg2}, {arg3}', flush=True)
9+
# return a value
10+
return arg1 + arg2 + arg3
11+
12+
# entry point for the program
13+
if __name__ == '__main__':
14+
# create the multiprocessing pool
15+
with Pool() as pool:
16+
# prepare task arguments
17+
args = [(i, i*2, i*3) for i in range(10)]
18+
# issue multiple tasks to the pool and process return values
19+
for result in pool.starmap(task, args):
20+
print(result)

src/lesson04_apply_async.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# SuperFastPython.com
2+
# example of executing a task with Pool.apply_async()
3+
from multiprocessing import Pool
4+
5+
# a task to execute in another process
6+
def task():
7+
print('This is another process', flush=True)
8+
9+
# entry point for the program
10+
if __name__ == '__main__':
11+
# create the multiprocessing pool
12+
with Pool() as pool:
13+
# issue a task asynchronously
14+
async_result = pool.apply_async(task)
15+
# wait for the task to complete
16+
async_result.wait()

src/lesson04_map_async.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# SuperFastPython.com
2+
# example of executing multiple task with Pool.map_async()
3+
from multiprocessing import Pool
4+
5+
# a task to execute in another process
6+
def task(arg):
7+
# report a message
8+
print(f'This is another process with {arg}', flush=True)
9+
# return a value
10+
return arg * 2
11+
12+
# entry point for the program
13+
if __name__ == '__main__':
14+
# create the multiprocessing pool
15+
with Pool() as pool:
16+
# issue multiple tasks to the pool
17+
async_result = pool.map_async(task, range(10))
18+
# process return values once all issued tasks have completed
19+
for result in async_result.get():
20+
print(result)

src/lesson04_starmap_async.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# SuperFastPython.com
2+
# example of executing multiple tasks with multiple arguments
3+
from multiprocessing import Pool
4+
5+
# a task to execute in another process
6+
def task(arg1, arg2, arg3):
7+
# report a message
8+
print(f'This is another process with {arg1}, {arg2}, {arg3}', flush=True)
9+
# return a value
10+
return arg1 + arg2 + arg3
11+
12+
# entry point for the program
13+
if __name__ == '__main__':
14+
# create the multiprocessing pool
15+
with Pool() as pool:
16+
# prepare task arguments
17+
args = [(i, i*2, i*3) for i in range(10)]
18+
# issue multiple tasks to the pool
19+
async_result = pool.starmap_async(task, args)
20+
# process return values
21+
for result in async_result.get():
22+
print(result)

src/lesson05_imap.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# SuperFastPython.com
2+
# example of executing multiple task with Pool.imap()
3+
from time import sleep
4+
from random import random
5+
from multiprocessing import Pool
6+
7+
# a task to execute in another process
8+
def task(arg):
9+
# block for a random fraction of a second
10+
sleep(random())
11+
# report a message
12+
print(f'This is another process with {arg}', flush=True)
13+
# return a value
14+
return arg * 2
15+
16+
# entry point for the program
17+
if __name__ == '__main__':
18+
# create the multiprocessing pool
19+
with Pool(4) as pool:
20+
# issue multiple tasks to the pool and process return values
21+
for result in pool.imap(task, range(10)):
22+
print(result)

src/lesson05_imap_unordered.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# SuperFastPython.com
2+
# example of executing multiple task with Pool.imap_unordered()
3+
from time import sleep
4+
from random import random
5+
from multiprocessing import Pool
6+
7+
# a task to execute in another process
8+
def task(arg):
9+
# block for a random fraction of a second
10+
sleep(random())
11+
# report a message
12+
print(f'This is another process with {arg}', flush=True)
13+
# return a value
14+
return arg * 2
15+
16+
# entry point for the program
17+
if __name__ == '__main__':
18+
# create the multiprocessing pool
19+
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)

src/lesson06_asyncresult.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# SuperFastPython.com
2+
# example of checking the status and handling the result of an async task
3+
from time import sleep
4+
from random import random
5+
from multiprocessing import Pool
6+
7+
# a task to execute in another process
8+
def task():
9+
# loop a few times to simulate a slow task
10+
for i in range(10):
11+
# generate a random value between 0 and 1
12+
value = random()
13+
# block for a fraction of a second
14+
sleep(value)
15+
# report a message
16+
print(f'>{i} got {value}', flush=True)
17+
18+
# entry point for the program
19+
if __name__ == '__main__':
20+
# create the multiprocessing pool
21+
with Pool() as pool:
22+
# issue a task asynchronously
23+
async_result = pool.apply_async(task)
24+
# wait until the task is complete
25+
while not async_result.ready():
26+
# report a message
27+
print('Main process waiting...')
28+
# block for a moment
29+
async_result.wait(timeout=1)
30+
# report if the task was successful
31+
if async_result.successful():
32+
print('Task was successful.')

src/lesson06_callback.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# SuperFastPython.com
2+
# example of a callback function for Pool.apply_async()
3+
from random import random
4+
from time import sleep
5+
from multiprocessing.pool import Pool
6+
7+
# result callback function
8+
def result_callback(return_value):
9+
print(f'Callback got: {return_value}', flush=True)
10+
11+
# task executed in a worker process
12+
def task(identifier):
13+
# generate a value
14+
value = random()
15+
# report a message
16+
print(f'Task {identifier} executing with {value}', flush=True)
17+
# block for a moment
18+
sleep(value)
19+
# return the generated value
20+
return value
21+
22+
# protect the entry point
23+
if __name__ == '__main__':
24+
# create and configure the multiprocessing pool
25+
with Pool() as pool:
26+
# issue tasks to the multiprocessing pool
27+
result = pool.apply_async(task, args=(0,), callback=result_callback)
28+
# close the multiprocessing pool
29+
pool.close()
30+
# wait for all tasks to complete
31+
pool.join()

0 commit comments

Comments
 (0)