**CPython interpreter can only run a single thread at a time. Using multiple threads won't result in a faster execution, unless at least one of the threads contains an I/O operation.**
```python
from threading import Thread, Timer, RLock, Semaphore, Event, Barrier
from threading import Thread, RLock, Semaphore, Event, Barrier
from concurrent.futures import ThreadPoolExecutor, as_completed
```
@ -2113,7 +2113,6 @@ from concurrent.futures import ThreadPoolExecutor, as_completed
```
* **Use `'kwargs=<dict>'` to pass keyword arguments to the function.**
* **Use `'daemon=True'`, or the program will not be able to exit while the thread is alive.**
* **To delay thread execution use `'Timer(seconds, <func>)'` instead of Thread().**
### Lock
```python
@ -2158,7 +2157,7 @@ with <lock>: # Enters the block by calling acq
<bool> = <Future>.cancel() # Cancels or returns False if running/finished.
<iter> = as_completed(<coll_of_Futures>) # Next() waits for next completed Future.
```
* **Map() and as\_completed() also accept 'timeout'. It causes futures.TimeoutError when next() is called. Map() times from original call and as_completed() from first call to next().**
* **Map() and as\_completed() also accept 'timeout'. It causes futures.TimeoutError when next() is called/blocking. Map() times from original call and as_completed() from first call to next(). As\_completed() fails if next() is called too late, even if thread finished on time.**
* **Exceptions that happen inside threads are raised when next() is called on map's iterator or when result() is called on a Future. Its exception() method returns exception or None.**
* **ProcessPoolExecutor provides true parallelism, but everything sent to/from workers must be [pickable](#pickle). Queues must be sent using executor's 'initargs' and 'initializer' parameters.**
<deque>.rotate(n=<spanclass="hljs-number">1</span>) <spanclass="hljs-comment"># Last element becomes first.</span>
<el> = <deque>.popleft() <spanclass="hljs-comment"># Raises IndexError if deque is empty.</span>
</code></pre>
<div><h2id="threading"><ahref="#threading"name="threading">#</a>Threading</h2><p><strong>CPython interpreter can only run a single thread at a time. Using multiple threads won't result in a faster execution, unless at least one of the threads contains an I/O operation.</strong></p><pre><codeclass="python language-python hljs"><spanclass="hljs-keyword">from</span> threading <spanclass="hljs-keyword">import</span> Thread, Timer, RLock, Semaphore, Event, Barrier
<div><h2id="threading"><ahref="#threading"name="threading">#</a>Threading</h2><p><strong>CPython interpreter can only run a single thread at a time. Using multiple threads won't result in a faster execution, unless at least one of the threads contains an I/O operation.</strong></p><pre><codeclass="python language-python hljs"><spanclass="hljs-keyword">from</span> threading <spanclass="hljs-keyword">import</span> Thread, RLock, Semaphore, Event, Barrier
<li><strong>Use <codeclass="python hljs"><spanclass="hljs-string">'kwargs=<dict>'</span></code> to pass keyword arguments to the function.</strong></li>
<li><strong>Use <codeclass="python hljs"><spanclass="hljs-string">'daemon=True'</span></code>, or the program will not be able to exit while the thread is alive.</strong></li>
<li><strong>To delay thread execution use <codeclass="python hljs"><spanclass="hljs-string">'Timer(seconds, <func>)'</span></code> instead of Thread().</strong></li>
</ul>
<div><h3id="lock">Lock</h3><pre><codeclass="python language-python hljs"><lock> = RLock() <spanclass="hljs-comment"># Lock that can only be released by acquirer.</span>
<lock>.acquire() <spanclass="hljs-comment"># Waits for the lock to be available.</span>
<iter> = as_completed(<coll_of_Futures>) <spanclass="hljs-comment"># Next() waits for next completed Future.</span>
</code></pre>
<ul>
<li><strong>Map() and as_completed() also accept 'timeout'. It causes futures.TimeoutError when next() is called. Map() times from original call and as_completed() from first call to next().</strong></li>
<li><strong>Map() and as_completed() also accept 'timeout'. It causes futures.TimeoutError when next() is called/blocking. Map() times from original call and as_completed() from first call to next(). As_completed() fails if next() is called too late, even if thread finished on time.</strong></li>
<li><strong>Exceptions that happen inside threads are raised when next() is called on map's iterator or when result() is called on a Future. Its exception() method returns exception or None.</strong></li>
<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.</strong></li>