or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Do you want to remove this version name and description?
Syncing
xxxxxxxxxx
Effective Python 2E
Chapter 7: Concurrency and Parallelism
subprocess
andthreading
(Items 52–57)David Ye @ Houzz
Outline
subprocess
(Item 52)threading
(Items 53–55, 57)When Concurrency is Necessary
Concurrency (並行):
Parallelism (平行):
Item 56: Know How to Recognize When Concurrency Is Necessary
subprocess
Item 52: Use
subprocess
to Manage Child ProcessesPEP-324
os.system
etc.subprocess.run
: run and waitsubprocess.Popen
will not waitPopen.poll()
None
Popen.communicate
wait and get outputmanage streams
Chaining Parallel Processes
pipe one stdout to another stdin
Timeout Subprocess
threading
Item 53: Use Threads for Blocking I/O, Avoid for Parallelism
Item 54: Use
Lock
to Prevent Data Races in ThreadsGIL is thread-safe on bytecodes, not Python data structure.
+=
is not atomicItem 55: Use
Queue
to Coordinate Work Between ThreadsI/O blocked pipeline, e.g.
download
->resize
->upload
a Queue can:
Queue.task_done()
mark lastget
doneQueue.join()
wait for allput
doneTask Pipeline
An iterable queue
A worker
get
from this queue andput
to next queueStart work!
Use multiple worker for every stage
use the function:
Item 57: Avoid Creating New
Thread
Instances for On-demand Fan-outThread
instanceSolutions
The End
Thanks for listening!