Skip to content

Commit a6d9f3e

Browse files
first commit
0 parents  commit a6d9f3e

29 files changed

+693
-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: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Python Threading Jump-Start
2+
3+
![Python Threading Jump-Start](cover.png)
4+
5+
* <https://github.com/SuperFastPython/PythonThreadingJumpStart>
6+
7+
This repository provides all source code for the book:
8+
9+
* **Python Threading Jump-Start**: _Develop Concurrent IO-bound Programs And Work With The GIL_, Jason Brownlee, 2022.
10+
11+
12+
## Source Code
13+
You can access all Python .py files directly here:
14+
15+
* [src/](src/)
16+
17+
18+
19+
### Book Blurb
20+
21+
> Unlock concurrency with Python threads (and run 100s or 1,000s of tasks simultaneously).
22+
>
23+
> The threading module provides easy-to-use thread-based concurrency in Python.
24+
>
25+
> Unlike Python multiprocessing, the threading module is limited by the infamous Global Interpreter Lock (GIL).
26+
>
27+
> Critically, the GIL is released when performing blocking I/O. Additionally, threads can share memory make them perfectly suited to I/O-bound tasks such as reading and writing from files and socket connections.
28+
>
29+
> This is the API you need to use to make your code run faster.
30+
>
31+
> Introducing: "Python Threading Jump-Start". A new book designed to teach you the threading module in Python, super fast!
32+
>
33+
> You will get a rapid-paced, 7-part course to get you started and make you awesome at using the threading API.
34+
>
35+
> Each of the 7 lessons was carefully designed to teach one critical aspect of the threading module, with explanations, code snippets and worked examples.
36+
>
37+
> You will discover:
38+
>
39+
> * How to choose tasks that are well suited to threads.
40+
> * How to create and run new threads.
41+
> * How to locate and query running threads.
42+
> * How to use locks, semaphores, barriers and more.
43+
> * How to share data between threads using queues.
44+
> * How to execute ad hoc tasks with reusable worker threads.
45+
> * How to gracefully stop and forcefully kill threads.
46+
>
47+
> 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.
48+
>
49+
> Stop copy-pasting code from StackOverflow answers.
50+
>
51+
> Learn Python concurrency correctly, step-by-step.
52+

cover.png

673 KB
Loading

src/lesson02_extend_thread.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 thread class
3+
from time import sleep
4+
from threading import Thread
5+
6+
# custom thread class
7+
class CustomThread(Thread):
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 thread')
14+
15+
# protect the entry point
16+
if __name__ == '__main__':
17+
# create the thread
18+
thread = CustomThread()
19+
# start the thread
20+
thread.start()
21+
# wait for the thread to finish
22+
print('Waiting for the thread to finish')
23+
thread.join()

src/lesson02_run_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 thread
3+
from time import sleep
4+
from threading import Thread
5+
6+
# custom function to be executed in a new thread
7+
def task():
8+
# block for a moment
9+
sleep(1)
10+
# report a message
11+
print('This is from another thread')
12+
13+
# protect the entry point
14+
if __name__ == '__main__':
15+
# create a new thread instance
16+
thread = Thread(target=task)
17+
# start executing the function in the new thread
18+
thread.start()
19+
# wait for the thread to finish
20+
print('Waiting for the thread...')
21+
thread.join()

src/lesson02_thread_local.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 sharing thread-local storage
3+
from time import sleep
4+
from threading import Thread
5+
from threading import local
6+
7+
# custom function executed in a new thread
8+
def task(shared_local):
9+
# block for a moment to simulate work
10+
sleep(1)
11+
# store a private variable on the thread local
12+
shared_local.value = 33
13+
# report the stored value
14+
print(f'Thread stored: {shared_local.value}')
15+
16+
# protect the entry point
17+
if __name__ == '__main__':
18+
# create a shared thread-local instance
19+
local_storage = local()
20+
# store a private variable on the thread local
21+
local_storage.value = 100
22+
# report the stored value
23+
print(f'Main stored: {local_storage.value}')
24+
# create a new thread to run the custom function
25+
thread = Thread(target=task, args=(local_storage,))
26+
# start the new thread
27+
thread.start()
28+
# wait for the thread to terminate
29+
thread.join()
30+
# report the stored value
31+
print(f'Main sees: {local_storage.value}')

src/lesson03_current_thread.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 thread
3+
from threading import current_thread
4+
5+
# protect the entry point
6+
if __name__ == '__main__':
7+
# get the current thread
8+
thread = current_thread()
9+
# report details
10+
print(thread)

src/lesson03_daemon_thread.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 creating a daemon thread
3+
from threading import Thread
4+
5+
# protect the entry point
6+
if __name__ == '__main__':
7+
# create a daemon thread
8+
thread = Thread(daemon=True)
9+
# report if the thread is a daemon
10+
print(thread.daemon)

src/lesson03_enumerate_threads.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 threads
3+
from time import sleep
4+
from threading import Thread
5+
import threading
6+
7+
# custom function to be executed in a new thread
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 new threads
15+
threads = [Thread(target=task) for _ in range(5)]
16+
# start the new threads
17+
for thread in threads:
18+
thread.start()
19+
# get a list of all running threads
20+
running_threads = threading.enumerate()
21+
# report a count of active threads
22+
print(f'Active Threads: {len(running_threads)}')
23+
# report each in turn
24+
for thread in running_threads:
25+
print(thread)

src/lesson03_excepthook.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 handling a thread's unexpected exception
3+
from time import sleep
4+
from threading import Thread
5+
import threading
6+
7+
# custom exception hook
8+
def custom_hook(args):
9+
# report the failure
10+
print(f'Thread failed: {args.exc_value}')
11+
12+
# target function that raises an exception
13+
def task():
14+
# report a message
15+
print('Working...')
16+
# block for a moment
17+
sleep(1)
18+
# rise an "unexpected" exception
19+
raise Exception('Something bad happened')
20+
21+
# protect the entry point
22+
if __name__ == '__main__':
23+
# register the exception hook function
24+
threading.excepthook = custom_hook
25+
# create a thread
26+
thread = Thread(target=task)
27+
# run the thread
28+
thread.start()
29+
# wait for the thread to finish
30+
thread.join()
31+
# report that the main thread is not dead
32+
print('Continuing on...')

0 commit comments

Comments
 (0)