Browse Source

Threading

pull/140/head
Jure Šorn 2 years ago
parent
commit
f32d13ea7a
2 changed files with 4 additions and 2 deletions
  1. 3
      README.md
  2. 3
      index.html

3
README.md

@ -2092,7 +2092,7 @@ Threading
* **That is why using multiple threads won't result in a faster execution, unless at least one of the threads contains an I/O operation.** * **That is why 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, RLock, Semaphore, Event, Barrier from threading import Thread, RLock, Semaphore, Event, Barrier
from concurrent.futures import ThreadPoolExecutor
from concurrent.futures import ThreadPoolExecutor, as_completed
``` ```
### Thread ### Thread
@ -2139,6 +2139,7 @@ with <lock>: # Enters the block by calling acq
<Futr> = <Exec>.submit(<func>, <arg_1>, ...) # Starts a thread and returns its Future object. <Futr> = <Exec>.submit(<func>, <arg_1>, ...) # Starts a thread and returns its Future object.
<bool> = <Futr>.done() # Checks if the thread has finished executing. <bool> = <Futr>.done() # Checks if the thread has finished executing.
<obj> = <Futr>.result() # Waits for thread to finish and returns result. <obj> = <Futr>.result() # Waits for thread to finish and returns result.
<iter> = as_completed(<coll_of_Futr>) # Each Future is yielded as it completes.
``` ```
### Queue ### Queue

3
index.html

@ -1731,7 +1731,7 @@ CompletedProcess(args=[<span class="hljs-string">'bc'</span>, <span class="hljs-
<li><strong>CPython interpreter can only run a single thread at a time.</strong></li> <li><strong>CPython interpreter can only run a single thread at a time.</strong></li>
<li><strong>That is why using multiple threads won't result in a faster execution, unless at least one of the threads contains an I/O operation.</strong></li> <li><strong>That is why using multiple threads won't result in a faster execution, unless at least one of the threads contains an I/O operation.</strong></li>
</ul><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 </ul><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
<span class="hljs-keyword">from</span> concurrent.futures <span class="hljs-keyword">import</span> ThreadPoolExecutor, as_completed
</code></pre></div> </code></pre></div>
@ -1771,6 +1771,7 @@ CompletedProcess(args=[<span class="hljs-string">'bc'</span>, <span class="hljs-
&lt;Futr&gt; = &lt;Exec&gt;.submit(&lt;func&gt;, &lt;arg_1&gt;, ...) <span class="hljs-comment"># Starts a thread and returns its Future object.</span> &lt;Futr&gt; = &lt;Exec&gt;.submit(&lt;func&gt;, &lt;arg_1&gt;, ...) <span class="hljs-comment"># Starts a thread and returns its Future object.</span>
&lt;bool&gt; = &lt;Futr&gt;.done() <span class="hljs-comment"># Checks if the thread has finished executing.</span> &lt;bool&gt; = &lt;Futr&gt;.done() <span class="hljs-comment"># Checks if the thread has finished executing.</span>
&lt;obj&gt; = &lt;Futr&gt;.result() <span class="hljs-comment"># Waits for thread to finish and returns result.</span> &lt;obj&gt; = &lt;Futr&gt;.result() <span class="hljs-comment"># Waits for thread to finish and returns result.</span>
&lt;iter&gt; = as_completed(&lt;coll_of_Futr&gt;) <span class="hljs-comment"># Each Future is yielded as it completes.</span>
</code></pre> </code></pre>
<div><h3 id="queue">Queue</h3><p><strong>A thread-safe FIFO queue. For LIFO queue use LifoQueue.</strong></p><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> queue <span class="hljs-keyword">import</span> Queue <div><h3 id="queue">Queue</h3><p><strong>A thread-safe FIFO queue. For LIFO queue use LifoQueue.</strong></p><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> queue <span class="hljs-keyword">import</span> Queue
&lt;Queue&gt; = Queue(maxsize=<span class="hljs-number">0</span>) &lt;Queue&gt; = Queue(maxsize=<span class="hljs-number">0</span>)

Loading…
Cancel
Save