Skip to content

Commit c286611

Browse files
first commit
0 parents  commit c286611

20 files changed

Lines changed: 437 additions & 0 deletions

‎.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: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Python ThreadPool Jump-Start
2+
3+
![Python ThreadPool Jump-Start](cover.png)
4+
5+
* <https://github.com/SuperFastPython/PythonThreadPoolJumpStart>
6+
7+
This repository provides all source code for the book:
8+
9+
* **Python ThreadPool Jump-Start**: _Run Your IO-Bound Functions Concurrently With Just A Few Lines Of Code_, Jason Brownlee, 2022.
10+
11+
12+
## Source Code
13+
You can access all Python .py files directly here:
14+
15+
* [src/](src/)
16+
17+
## Get the Book
18+
19+
You can learn more about the book here:
20+
21+
* Coming soon.
22+
23+
### Book Blurb
24+
25+
> How much faster could your Python code run (if you used 100s of threads)?
26+
>
27+
> The ThreadPool class provides easy-to-use thread-based concurrency for IO-bound tasks.
28+
>
29+
> This is not some random third-party library, this is a class provided in the Python standard library (already installed on your system).
30+
>
31+
> This is the class you need to use to make your code run faster.
32+
>
33+
> There's just one problem. No one knows about it (or how to use it well).
34+
>
35+
> Introducing: "Python ThreadPool Jump-Start". A new book designed to teach you thread pools in Python, super fast!
36+
>
37+
> You will get a rapid-paced, 7-part course to get you started and make you awesome at using the ThreadPool.
38+
>
39+
> Each of the 7 lessons was carefully designed to teach one critical aspect of the ThreadPool, with explanations, code snippets and worked examples.
40+
>
41+
> 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.
42+
>
43+
> Stop copy-pasting code from StackOverflow answers.
44+
>
45+
> Learn Python concurrency correctly, step-by-step.

‎cover.png‎

678 KB
Loading

‎src/lesson01_new_thread.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# SuperFastPython.com
2+
# example of running a function in a new thread
3+
from threading import Thread
4+
5+
# custom function to be executed in a new thread
6+
def task():
7+
# report a message
8+
print('This is another thread')
9+
10+
# protect the entry point
11+
if __name__ == '__main__':
12+
# define a task to run in a new thread
13+
thread = Thread(target=task)
14+
# start the task in a new thread
15+
thread.start()
16+
# wait for the thread to terminate
17+
thread.join()

‎src/lesson01_threadpool.py‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# SuperFastPython.com
2+
# example running a function in the thread pool
3+
from multiprocessing.pool import ThreadPool
4+
5+
# custom function to be executed in a worker thread
6+
def task():
7+
# report a message
8+
print('This is another thread')
9+
10+
# protect the entry point
11+
if __name__ == '__main__':
12+
# create the thread pool
13+
with ThreadPool() 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 thread pool automatically

‎src/lesson02_default_config.py‎

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

‎src/lesson02_initializer.py‎

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

‎src/lesson02_num_threads.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 configuring a thread pool with many works
3+
from multiprocessing.pool import ThreadPool
4+
5+
# protect the entry point
6+
if __name__ == '__main__':
7+
# create a thread pool with many workers
8+
pool = ThreadPool(1000)
9+
# report the status of the thread pool
10+
print(pool)
11+
# close the thread pool
12+
pool.close()

‎src/lesson03_apply.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# SuperFastPython.com
2+
# example of executing a one-off task and waiting
3+
from multiprocessing.pool import ThreadPool
4+
5+
# custom function to be executed in a worker thread
6+
def task():
7+
# report a message
8+
print('This is another thread')
9+
10+
# protect the entry point
11+
if __name__ == '__main__':
12+
# create the thread pool
13+
with ThreadPool() as pool:
14+
# issue a task and wait for it to complete
15+
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 tasks and waiting
3+
from multiprocessing.pool import ThreadPool
4+
5+
# custom function to be executed in a worker thread
6+
def task(arg):
7+
# report a message
8+
print(f'From another thread {arg}')
9+
# return a value
10+
return arg * 2
11+
12+
# protect the entry point
13+
if __name__ == '__main__':
14+
# create the thread pool
15+
with ThreadPool() as pool:
16+
# issue multiple tasks and handle return values
17+
for result in pool.map(task, range(10)):
18+
print(result)

0 commit comments

Comments
 (0)