You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+17-17Lines changed: 17 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2258,15 +2258,15 @@ from concurrent.futures import ThreadPoolExecutor, as_completed
2258
2258
2259
2259
### Lock
2260
2260
```python
2261
-
<lock>= Lock/RLock() # RLock can only be released by acquirer.
2262
-
<lock>.acquire() #Waits for the lock to be available.
2263
-
<lock>.release() #Makes the lock available again.
2261
+
<lock>= Lock/RLock() # RLock can only be released by acquirer thread.
2262
+
<lock>.acquire() #Blocks (waits) until lock becomes available.
2263
+
<lock>.release() #It makes the acquired lock available again.
2264
2264
```
2265
2265
2266
2266
#### Or:
2267
2267
```python
2268
-
with<lock>: # Enters the block by calling acquire() and
2269
-
...#exits it with release(), even on error.
2268
+
with<lock>: # Enters the block by calling method acquire().
2269
+
...#Exits by calling release(), even on error.
2270
2270
```
2271
2271
2272
2272
### Semaphore, Event, Barrier
@@ -2278,24 +2278,24 @@ with <lock>: # Enters the block by calling acq
2278
2278
2279
2279
### Queue
2280
2280
```python
2281
-
<Queue>= queue.Queue(maxsize=0) # A thread-safe first-in-first-out queue.
2282
-
<Queue>.put(<el>) # Blocks until queue stops being full.
2283
-
<Queue>.put_nowait(<el>) # Raises queue.Full exception if full.
2284
-
<el>=<Queue>.get() # Blocks until queue stops being empty.
2285
-
<el>=<Queue>.get_nowait() # Raises queue.Empty exception if empty.
2281
+
<Queue>= queue.Queue(maxsize=0) # A first-in-first-out queue. It's thread safe.
2282
+
<Queue>.put(<obj>) # Call blocks until queue stops being full.
2283
+
<Queue>.put_nowait(<obj>) # Raises queue.Full exception if queue is full.
2284
+
<obj>=<Queue>.get() # Call blocks until queue stops being empty.
2285
+
<obj>=<Queue>.get_nowait() # Raises queue.Empty exception if it's empty.
2286
2286
```
2287
2287
2288
2288
### Thread Pool Executor
2289
2289
```python
2290
2290
<Exec>= ThreadPoolExecutor(max_workers=None) # Also `with ThreadPoolExecutor() as <name>: …`.
2291
2291
<iter>=<Exec>.map(<func>, <args_1>, ...) # Multithreaded and non-lazy map(). Keeps order.
2292
2292
<Futr>=<Exec>.submit(<func>, <arg_1>, ...) # Creates a thread and returns its Future obj.
2293
-
<Exec>.shutdown() # Waits for all submitted threads to finish.
2293
+
<Exec>.shutdown() # Waits for all threads to finish executing.
2294
2294
```
2295
2295
2296
2296
```python
2297
2297
<bool>=<Future>.done() # Checks if the thread has finished executing.
2298
-
<obj>=<Future>.result(timeout=None) #Waits for thread to finish and returns result.
2298
+
<obj>=<Future>.result(timeout=None) #Raises TimeoutError after 'timeout' seconds.
2299
2299
<bool>=<Future>.cancel() # Cancels or returns False if running/finished.
2300
2300
<iter>= as_completed(<coll_of_Futures>) # `next(<iter>)` returns next completed Future.
2301
2301
```
@@ -2306,8 +2306,8 @@ with <lock>: # Enters the block by calling acq
2306
2306
2307
2307
Coroutines
2308
2308
----------
2309
-
***Coroutines have a lot in common with threads, but unlike threads, they only give up control when they call another coroutine and they don’t use as much memory.**
2310
-
***Coroutine definition starts with `'async'` and its call with `'await'`.**
2309
+
***Coroutines have a lot in common with threads, but unlike threads, they only give up control when they call another coroutine and they don’t consume as much memory.**
2310
+
***Coroutine definition starts with `'async'` and its call with `'await'` keyword.**
2311
2311
***Use `'asyncio.run(<coroutine>)'` to start the first/main coroutine.**
2312
2312
2313
2313
```python
@@ -2316,7 +2316,7 @@ import asyncio as aio
2316
2316
2317
2317
```python
2318
2318
<coro>=<async_function>(<args>) # Creates a coroutine by calling async def function.
2319
-
<obj>=await<coroutine># Starts the coroutine and waits for its result.
2319
+
<obj>=await<coroutine># Starts the coroutine. Returns its result or None.
2320
2320
<task>= aio.create_task(<coroutine>) # Schedules it for execution. Always keep the task.
2321
2321
<obj>=await<task># Returns coroutine's result. Also <task>.cancel().
<li><strong>Use <codeclass="python hljs"><spanclass="hljs-string">'kwargs=<dict>'</span></code> to pass keyword arguments to the function.</strong></li>
1864
1864
<li><strong>Use <codeclass="python hljs"><spanclass="hljs-string">'daemon=True'</span></code>, or the program won't be able to exit while the thread is alive.</strong></li>
1865
1865
</ul>
1866
-
<div><h3id="lock">Lock</h3><pre><codeclass="python language-python hljs"><lock> = Lock/RLock() <spanclass="hljs-comment"># RLock can only be released by acquirer.</span>
1867
-
<lock>.acquire() <spanclass="hljs-comment"># Waits for the lock to be available.</span>
1868
-
<lock>.release() <spanclass="hljs-comment"># Makes the lock available again.</span>
1866
+
<div><h3id="lock">Lock</h3><pre><codeclass="python language-python hljs"><lock> = Lock/RLock() <spanclass="hljs-comment"># RLock can only be released by acquirer thread.</span>
1867
+
<lock>.acquire() <spanclass="hljs-comment"># Blocks (waits) until lock becomes available.</span>
1868
+
<lock>.release() <spanclass="hljs-comment"># It makes the acquired lock available again.</span>
1869
1869
</code></pre></div>
1870
1870
1871
-
<div><h4id="or-1">Or:</h4><pre><codeclass="python language-python hljs"><spanclass="hljs-keyword">with</span> <lock>: <spanclass="hljs-comment"># Enters the block by calling acquire() and</span>
1872
-
... <spanclass="hljs-comment"># exits it with release(), even on error.</span>
1871
+
<div><h4id="or-1">Or:</h4><pre><codeclass="python language-python hljs"><spanclass="hljs-keyword">with</span> <lock>: <spanclass="hljs-comment"># Enters the block by calling method acquire().</span>
1872
+
... <spanclass="hljs-comment"># Exits by calling release(), even on error.</span>
1873
1873
</code></pre></div>
1874
1874
1875
1875
<div><h3id="semaphoreeventbarrier">Semaphore, Event, Barrier</h3><pre><codeclass="python language-python hljs"><Semaphore> = Semaphore(value=<spanclass="hljs-number">1</span>) <spanclass="hljs-comment"># Lock that can be acquired by 'value' threads.</span>
1876
1876
<Event> = Event() <spanclass="hljs-comment"># Method wait() blocks until set() is called.</span>
1877
1877
<Barrier> = Barrier(n_times) <spanclass="hljs-comment"># Wait() blocks until it's called n times.</span>
1878
1878
</code></pre></div>
1879
1879
1880
-
<div><h3id="queue">Queue</h3><pre><codeclass="python language-python hljs"><Queue> = queue.Queue(maxsize=<spanclass="hljs-number">0</span>) <spanclass="hljs-comment"># A thread-safe first-in-first-out queue.</span>
1881
-
<Queue>.put(<el>) <spanclass="hljs-comment"># Blocks until queue stops being full.</span>
1882
-
<Queue>.put_nowait(<el>) <spanclass="hljs-comment"># Raises queue.Full exception if full.</span>
1883
-
<el> = <Queue>.get() <spanclass="hljs-comment"># Blocks until queue stops being empty.</span>
1884
-
<el> = <Queue>.get_nowait() <spanclass="hljs-comment"># Raises queue.Empty exception if empty.</span>
<Queue>.put(<obj>) <spanclass="hljs-comment"># Call blocks until queue stops being full.</span>
1882
+
<Queue>.put_nowait(<obj>) <spanclass="hljs-comment"># Raises queue.Full exception if queue is full.</span>
1883
+
<obj> = <Queue>.get() <spanclass="hljs-comment"># Call blocks until queue stops being empty.</span>
1884
+
<obj> = <Queue>.get_nowait() <spanclass="hljs-comment"># Raises queue.Empty exception if it's empty.</span>
1885
1885
</code></pre></div>
1886
1886
1887
1887
<div><h3id="threadpoolexecutor">Thread Pool Executor</h3><pre><codeclass="python language-python hljs"><Exec> = ThreadPoolExecutor(max_workers=<spanclass="hljs-keyword">None</span>) <spanclass="hljs-comment"># Also `with ThreadPoolExecutor() as <name>: …`.</span>
<Futr> = <Exec>.submit(<func>, <arg_1>, ...) <spanclass="hljs-comment"># Creates a thread and returns its Future obj.</span>
1890
-
<Exec>.shutdown() <spanclass="hljs-comment"># Waits for all submitted threads to finish.</span>
1890
+
<Exec>.shutdown() <spanclass="hljs-comment"># Waits for all threads to finish executing.</span>
1891
1891
</code></pre></div>
1892
1892
1893
1893
<pre><codeclass="python language-python hljs"><bool> = <Future>.done() <spanclass="hljs-comment"># Checks if the thread has finished executing.</span>
1894
-
<obj> = <Future>.result(timeout=<spanclass="hljs-keyword">None</span>) <spanclass="hljs-comment"># Waits for thread to finish and returns result.</span>
1894
+
<obj> = <Future>.result(timeout=<spanclass="hljs-keyword">None</span>) <spanclass="hljs-comment"># Raises TimeoutError after 'timeout' seconds.</span>
1895
1895
<bool> = <Future>.cancel() <spanclass="hljs-comment"># Cancels or returns False if running/finished.</span>
1896
1896
<iter> = as_completed(<coll_of_Futures>) <spanclass="hljs-comment"># `next(<iter>)` returns next completed Future.</span>
<li><strong>ProcessPoolExecutor provides true parallelism but: everything sent to/from workers must be <ahref="#pickle">pickable</a>, queues must be sent using executor's 'initargs' and 'initializer' parameters, and executor should only be reachable via <codeclass="python hljs"><spanclass="hljs-string">'if __name__ == "__main__": ...'</span></code>.</strong></li>
<li><strong>Coroutines have a lot in common with threads, but unlike threads, they only give up control when they call another coroutine and they don’t use as much memory.</strong></li>
1905
-
<li><strong>Coroutine definition starts with <codeclass="python hljs"><spanclass="hljs-string">'async'</span></code> and its call with <codeclass="python hljs"><spanclass="hljs-string">'await'</span></code>.</strong></li>
1904
+
<li><strong>Coroutines have a lot in common with threads, but unlike threads, they only give up control when they call another coroutine and they don’t consume as much memory.</strong></li>
1905
+
<li><strong>Coroutine definition starts with <codeclass="python hljs"><spanclass="hljs-string">'async'</span></code> and its call with <codeclass="python hljs"><spanclass="hljs-string">'await'</span></code> keyword.</strong></li>
1906
1906
<li><strong>Use <codeclass="python hljs"><spanclass="hljs-string">'asyncio.run(<coroutine>)'</span></code> to start the first/main coroutine.</strong></li>
<pre><codeclass="python language-python hljs"><coro> = <async_function>(<args>) <spanclass="hljs-comment"># Creates a coroutine by calling async def function.</span>
1912
-
<obj> = <spanclass="hljs-keyword">await</span> <coroutine> <spanclass="hljs-comment"># Starts the coroutine and waits for its result.</span>
1912
+
<obj> = <spanclass="hljs-keyword">await</span> <coroutine> <spanclass="hljs-comment"># Starts the coroutine. Returns its result or None.</span>
1913
1913
<task> = aio.create_task(<coroutine>) <spanclass="hljs-comment"># Schedules it for execution. Always keep the task.</span>
1914
1914
<obj> = <spanclass="hljs-keyword">await</span> <task> <spanclass="hljs-comment"># Returns coroutine's result. Also <task>.cancel().</span>
0 commit comments