Skip to content

Commit 14864bb

Browse files
first commit
0 parents  commit 14864bb

27 files changed

+655
-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: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Python Multiprocessing Jump-Start
2+
3+
![Python Multiprocessing Jump-Start](cover.png)
4+
5+
* <https://github.com/SuperFastPython/PythonMultiprocessingJumpStart>
6+
7+
This repository provides all source code for the book:
8+
9+
* **Python Multiprocessing Jump-Start**: _Develop Parallel Programs, Side-Step the GIL, and Use All CPU Cores_, Jason Brownlee, 2022.
10+
11+
12+
## Source Code
13+
You can access all Python .py files directly here:
14+
15+
* [src/](src/)
16+
17+
### Book Blurb
18+
19+
> Unlock parallel Python programming (and run your code on all CPUs).
20+
>
21+
> The multiprocessing module provides easy-to-use process-based concurrency in Python.
22+
>
23+
> Unlike Python threading, multiprocessing side-steps the infamous Global Interpreter Lock (GIL), allowing full parallelism in Python.
24+
>
25+
> This is not some random third-party library, this is an API provided in the Python standard library (already installed on your system).
26+
>
27+
> This is the API you need to use to make your code run faster.
28+
>
29+
> There's just one problem. Few developers know about it (or how to use it well).
30+
>
31+
> Introducing: "Python Multiprocessing Jump-Start". A new book designed to teach you the multiprocessing module in Python, super fast!
32+
>
33+
> You will get a fast-paced, 7-part course to get you started and make you awesome at using the multiprocessing API.
34+
>
35+
> Each of the 7 lessons was carefully designed to teach one critical aspect of the multiprocessing module, with explanations, code snippets and worked examples.
36+
>
37+
> Each lesson ends with an exercise for you to complete to confirm you understand the topic, a summary of what was learned, and links for further reading if you want to go deeper.
38+
>
39+
> Stop copy-pasting code from StackOverflow answers.
40+
>
41+
> Learn Python concurrency correctly, step-by-step.

cover.png

685 KB
Loading

src/lesson02_custom_function.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# SuperFastPython.com
2+
# example of running a function in a new child process
3+
from time import sleep
4+
from multiprocessing import Process
5+
6+
# custom function to be executed in a child process
7+
def task():
8+
# block for a moment
9+
sleep(1)
10+
# report a message
11+
print('This is from another process', flush=True)
12+
13+
# protect the entry point
14+
if __name__ == '__main__':
15+
# create a new process instance
16+
process = Process(target=task)
17+
# start executing the function in the process
18+
process.start()
19+
# wait for the process to finish
20+
print('Waiting for the process...')
21+
process.join()

src/lesson02_extend_process.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# SuperFastPython.com
2+
# example of extending the process class
3+
from time import sleep
4+
from multiprocessing import Process
5+
6+
# custom process class
7+
class CustomProcess(Process):
8+
# override the run function
9+
def run(self):
10+
# block for a moment
11+
sleep(1)
12+
# report a message
13+
print('This is another process', flush=True)
14+
15+
# protect the entry point
16+
if __name__ == '__main__':
17+
# create the process
18+
process = CustomProcess()
19+
# start the process
20+
process.start()
21+
# wait for the process to finish
22+
print('Waiting for the process to finish')
23+
process.join()

src/lesson03_active_children.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# SuperFastPython.com
2+
# example of getting a list of active child processes
3+
from time import sleep
4+
from multiprocessing import active_children
5+
from multiprocessing import Process
6+
7+
# custom function to be executed in a child process
8+
def task():
9+
# block for a moment
10+
sleep(1)
11+
12+
# protect the entry point
13+
if __name__ == '__main__':
14+
# create a number of child processes
15+
processes = [Process(target=task) for _ in range(5)]
16+
# start the child processes
17+
for process in processes:
18+
process.start()
19+
# get a list of all active child processes
20+
children = active_children()
21+
# report a count of active children
22+
print(f'Active Children Count: {len(children)}')
23+
# report each in turn
24+
for child in children:
25+
print(child)

src/lesson03_current_process.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# SuperFastPython.com
2+
# example of getting access to the current process
3+
from multiprocessing import current_process
4+
5+
# protect the entry point
6+
if __name__ == '__main__':
7+
# get the current process
8+
process = current_process()
9+
# report details
10+
print(process)

src/lesson03_daemon.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# SuperFastPython.com
2+
# example of setting a process to be a daemon
3+
from multiprocessing import Process
4+
5+
# protect the entry point
6+
if __name__ == '__main__':
7+
# create a daemon process
8+
process = Process(daemon=True)
9+
# report if the process is a daemon
10+
print(process.daemon)

src/lesson03_exitcode.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# SuperFastPython.com
2+
# example of checking the exit status of a child process
3+
from time import sleep
4+
from multiprocessing import Process
5+
6+
# custom function to be executed in a child process
7+
def task():
8+
# block for a moment
9+
sleep(1)
10+
11+
# protect the entry point
12+
if __name__ == '__main__':
13+
# create the process
14+
process = Process(target=task)
15+
# report the exit status
16+
print(process.exitcode)
17+
# start the process
18+
process.start()
19+
# report the exit status
20+
print(process.exitcode)
21+
# wait for the process to finish
22+
process.join()
23+
# report the exit status
24+
print(process.exitcode)

src/lesson03_isalive.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# SuperFastPython.com
2+
# example of assessing whether a process is alive
3+
from multiprocessing import Process
4+
5+
# protect the entry point
6+
if __name__ == '__main__':
7+
# create the process
8+
process = Process()
9+
# report the process is alive
10+
print(process.is_alive())

0 commit comments

Comments
 (0)