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)

0 commit comments

Comments
 (0)