Browse Source

Threading

pull/187/head
Jure Šorn 6 months ago
parent
commit
baa23bad86
2 changed files with 6 additions and 8 deletions
  1. 5
      README.md
  2. 9
      index.html

5
README.md

@ -2100,7 +2100,7 @@ Threading
--------- ---------
**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.** **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 ```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 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 `'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.** * **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 ### Lock
```python ```python
@ -2158,7 +2157,7 @@ with <lock>: # Enters the block by calling acq
<bool> = <Future>.cancel() # Cancels or returns False if running/finished. <bool> = <Future>.cancel() # Cancels or returns False if running/finished.
<iter> = as_completed(<coll_of_Futures>) # Next() waits for next completed Future. <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.** * **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.** * **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.**

9
index.html

@ -54,7 +54,7 @@
<body> <body>
<header> <header>
<aside>May 16, 2024</aside>
<aside>May 17, 2024</aside>
<a href="https://gto76.github.io" rel="author">Jure Šorn</a> <a href="https://gto76.github.io" rel="author">Jure Šorn</a>
</header> </header>
@ -1723,7 +1723,7 @@ CompletedProcess(args=[<span class="hljs-string">'bc'</span>, <span class="hljs-
&lt;deque&gt;.rotate(n=<span class="hljs-number">1</span>) <span class="hljs-comment"># Last element becomes first.</span> &lt;deque&gt;.rotate(n=<span class="hljs-number">1</span>) <span class="hljs-comment"># Last element becomes first.</span>
&lt;el&gt; = &lt;deque&gt;.popleft() <span class="hljs-comment"># Raises IndexError if deque is empty.</span> &lt;el&gt; = &lt;deque&gt;.popleft() <span class="hljs-comment"># Raises IndexError if deque is empty.</span>
</code></pre> </code></pre>
<div><h2 id="threading"><a href="#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><code class="python language-python hljs"><span class="hljs-keyword">from</span> threading <span class="hljs-keyword">import</span> Thread, Timer, RLock, Semaphore, Event, Barrier
<div><h2 id="threading"><a href="#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><code class="python language-python hljs"><span class="hljs-keyword">from</span> threading <span class="hljs-keyword">import</span> Thread, RLock, Semaphore, Event, Barrier
<span class="hljs-keyword">from</span> concurrent.futures <span class="hljs-keyword">import</span> ThreadPoolExecutor, as_completed <span class="hljs-keyword">from</span> concurrent.futures <span class="hljs-keyword">import</span> ThreadPoolExecutor, as_completed
</code></pre></div> </code></pre></div>
@ -1737,7 +1737,6 @@ CompletedProcess(args=[<span class="hljs-string">'bc'</span>, <span class="hljs-
<ul> <ul>
<li><strong>Use <code class="python hljs"><span class="hljs-string">'kwargs=&lt;dict&gt;'</span></code> to pass keyword arguments to the function.</strong></li> <li><strong>Use <code class="python hljs"><span class="hljs-string">'kwargs=&lt;dict&gt;'</span></code> to pass keyword arguments to the function.</strong></li>
<li><strong>Use <code class="python hljs"><span class="hljs-string">'daemon=True'</span></code>, or the program will not be able to exit while the thread is alive.</strong></li> <li><strong>Use <code class="python hljs"><span class="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 <code class="python hljs"><span class="hljs-string">'Timer(seconds, &lt;func&gt;)'</span></code> instead of Thread().</strong></li>
</ul> </ul>
<div><h3 id="lock">Lock</h3><pre><code class="python language-python hljs">&lt;lock&gt; = RLock() <span class="hljs-comment"># Lock that can only be released by acquirer.</span> <div><h3 id="lock">Lock</h3><pre><code class="python language-python hljs">&lt;lock&gt; = RLock() <span class="hljs-comment"># Lock that can only be released by acquirer.</span>
&lt;lock&gt;.acquire() <span class="hljs-comment"># Waits for the lock to be available.</span> &lt;lock&gt;.acquire() <span class="hljs-comment"># Waits for the lock to be available.</span>
@ -1772,7 +1771,7 @@ CompletedProcess(args=[<span class="hljs-string">'bc'</span>, <span class="hljs-
&lt;iter&gt; = as_completed(&lt;coll_of_Futures&gt;) <span class="hljs-comment"># Next() waits for next completed Future.</span> &lt;iter&gt; = as_completed(&lt;coll_of_Futures&gt;) <span class="hljs-comment"># Next() waits for next completed Future.</span>
</code></pre> </code></pre>
<ul> <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>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 <a href="#pickle">pickable</a>. Queues must be sent using executor's 'initargs' and 'initializer' parameters.</strong></li> <li><strong>ProcessPoolExecutor provides true parallelism, but everything sent to/from workers must be <a href="#pickle">pickable</a>. Queues must be sent using executor's 'initargs' and 'initializer' parameters.</strong></li>
</ul> </ul>
@ -2933,7 +2932,7 @@ $ deactivate <span class="hljs-comment"># Deactivates the activ
<footer> <footer>
<aside>May 16, 2024</aside>
<aside>May 17, 2024</aside>
<a href="https://gto76.github.io" rel="author">Jure Šorn</a> <a href="https://gto76.github.io" rel="author">Jure Šorn</a>
</footer> </footer>

Loading…
Cancel
Save