Skip to content

Commit 7760e20

Browse files
committed
Threading
1 parent e3cb5f2 commit 7760e20

File tree

2 files changed

+36
-41
lines changed

2 files changed

+36
-41
lines changed

README.md

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2130,36 +2130,32 @@ with <lock>: # Enters the block by calling acq
21302130
<Barrier> = Barrier(n_times) # Wait() blocks until it's called n_times.
21312131
```
21322132

2133-
### Thread Pool Executor
2134-
* **Object that manages thread execution.**
2135-
* **An object with the same interface called ProcessPoolExecutor provides true parallelism by running a separate interpreter in each process. All arguments must be [pickable](#pickle).**
2136-
2133+
### Queue
21372134
```python
2138-
<Exec> = ThreadPoolExecutor(max_workers=None) # Or: `with ThreadPoolExecutor() as <name>: …`
2139-
<Exec>.shutdown(wait=True) # Blocks until all threads finish executing.
2135+
<Queue> = queue.Queue(maxsize=0) # A thread-safe FIFO queue. Also LifoQueue.
2136+
<Queue>.put(<el>) # Blocks until queue stops being full.
2137+
<Queue>.put_nowait(<el>) # Raises queue.Full exception if full.
2138+
<el> = <Queue>.get() # Blocks until queue stops being empty.
2139+
<el> = <Queue>.get_nowait() # Raises queue.Empty exception if empty.
21402140
```
21412141

2142+
### Thread Pool Executor
21422143
```python
2143-
<iter> = <Exec>.map(<func>, <args_1>, ...) # A multithreaded and non-lazy map().
2144+
<Exec> = ThreadPoolExecutor(max_workers=None) # Or: `with ThreadPoolExecutor() as <name>: …`
2145+
<iter> = <Exec>.map(<func>, <args_1>, ...) # A multithreaded non-lazy map(). Keeps order.
21442146
<Futr> = <Exec>.submit(<func>, <arg_1>, ...) # Starts a thread and returns its Future object.
2145-
<bool> = <Futr>.done() # Checks if the thread has finished executing.
2146-
<obj> = <Futr>.result() # Waits for thread to finish and returns result.
2147-
<iter> = as_completed(<coll_of_Futr>) # Each Future is yielded as it completes.
2148-
```
2149-
2150-
### Queue
2151-
**A thread-safe FIFO queue. For LIFO queue use LifoQueue.**
2152-
```python
2153-
from queue import Queue
2154-
<Queue> = Queue(maxsize=0)
2147+
<Exec>.shutdown(wait=True) # Blocks until all threads finish executing.
21552148
```
21562149

21572150
```python
2158-
<Queue>.put(<el>) # Blocks until queue stops being full.
2159-
<Queue>.put_nowait(<el>) # Raises queue.Full exception if full.
2160-
<el> = <Queue>.get() # Blocks until queue stops being empty.
2161-
<el> = <Queue>.get_nowait() # Raises queue.Empty exception if empty.
2151+
<bool> = <Future>.done() # Checks if the thread has finished executing.
2152+
<obj> = <Future>.result(timeout=None) # Waits for thread to finish and returns result.
2153+
<bool> = <Future>.cancel() # Returns False if thread is already running.
2154+
<iter> = as_completed(<coll_of_Futures>) # Each Future is yielded as it completes.
21622155
```
2156+
* **Map() and as_completed() also accept 'timeout' argument that causes TimeoutError if result isn't available in 'timeout' seconds after next() is called.**
2157+
* **Exceptions that happen inside threads are raised when next() is called on map's iterator or when result() is called on a Future. It's exception() method returns exception or None.**
2158+
* **An object with the same interface called ProcessPoolExecutor provides true parallelism by running a separate interpreter in each process. Arguments and results must be [pickable](#pickle).**
21632159

21642160

21652161
Operator

index.html

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454

5555
<body>
5656
<header>
57-
<aside>May 26, 2023</aside>
57+
<aside>May 27, 2023</aside>
5858
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
5959
</header>
6060

@@ -1762,30 +1762,29 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment
17621762
&lt;Barrier&gt; = Barrier(n_times) <span class="hljs-comment"># Wait() blocks until it's called n_times.</span>
17631763
</code></pre></div>
17641764

1765-
<div><h3 id="threadpoolexecutor">Thread Pool Executor</h3><ul>
1766-
<li><strong>Object that manages thread execution.</strong></li>
1767-
<li><strong>An object with the same interface called ProcessPoolExecutor provides true parallelism by running a separate interpreter in each process. All arguments must be <a href="#pickle">pickable</a>.</strong></li>
1768-
</ul><pre><code class="python language-python hljs">&lt;Exec&gt; = ThreadPoolExecutor(max_workers=<span class="hljs-keyword">None</span>) <span class="hljs-comment"># Or: `with ThreadPoolExecutor() as &lt;name&gt;: …`</span>
1769-
&lt;Exec&gt;.shutdown(wait=<span class="hljs-keyword">True</span>) <span class="hljs-comment"># Blocks until all threads finish executing.</span>
1765+
<div><h3 id="queue">Queue</h3><pre><code class="python language-python hljs">&lt;Queue&gt; = queue.Queue(maxsize=<span class="hljs-number">0</span>) <span class="hljs-comment"># A thread-safe FIFO queue. Also LifoQueue.</span>
1766+
&lt;Queue&gt;.put(&lt;el&gt;) <span class="hljs-comment"># Blocks until queue stops being full.</span>
1767+
&lt;Queue&gt;.put_nowait(&lt;el&gt;) <span class="hljs-comment"># Raises queue.Full exception if full.</span>
1768+
&lt;el&gt; = &lt;Queue&gt;.get() <span class="hljs-comment"># Blocks until queue stops being empty.</span>
1769+
&lt;el&gt; = &lt;Queue&gt;.get_nowait() <span class="hljs-comment"># Raises queue.Empty exception if empty.</span>
17701770
</code></pre></div>
17711771

1772-
1773-
<pre><code class="python language-python hljs">&lt;iter&gt; = &lt;Exec&gt;.map(&lt;func&gt;, &lt;args_1&gt;, ...) <span class="hljs-comment"># A multithreaded and non-lazy map().</span>
1772+
<div><h3 id="threadpoolexecutor">Thread Pool Executor</h3><pre><code class="python language-python hljs">&lt;Exec&gt; = ThreadPoolExecutor(max_workers=<span class="hljs-keyword">None</span>) <span class="hljs-comment"># Or: `with ThreadPoolExecutor() as &lt;name&gt;: …`</span>
1773+
&lt;iter&gt; = &lt;Exec&gt;.map(&lt;func&gt;, &lt;args_1&gt;, ...) <span class="hljs-comment"># A multithreaded non-lazy map(). Keeps order.</span>
17741774
&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>
1775-
&lt;bool&gt; = &lt;Futr&gt;.done() <span class="hljs-comment"># Checks if the thread has finished executing.</span>
1776-
&lt;obj&gt; = &lt;Futr&gt;.result() <span class="hljs-comment"># Waits for thread to finish and returns result.</span>
1777-
&lt;iter&gt; = as_completed(&lt;coll_of_Futr&gt;) <span class="hljs-comment"># Each Future is yielded as it completes.</span>
1778-
</code></pre>
1779-
<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
1780-
&lt;Queue&gt; = Queue(maxsize=<span class="hljs-number">0</span>)
1775+
&lt;Exec&gt;.shutdown(wait=<span class="hljs-keyword">True</span>) <span class="hljs-comment"># Blocks until all threads finish executing.</span>
17811776
</code></pre></div>
17821777

1783-
1784-
<pre><code class="python language-python hljs">&lt;Queue&gt;.put(&lt;el&gt;) <span class="hljs-comment"># Blocks until queue stops being full.</span>
1785-
&lt;Queue&gt;.put_nowait(&lt;el&gt;) <span class="hljs-comment"># Raises queue.Full exception if full.</span>
1786-
&lt;el&gt; = &lt;Queue&gt;.get() <span class="hljs-comment"># Blocks until queue stops being empty.</span>
1787-
&lt;el&gt; = &lt;Queue&gt;.get_nowait() <span class="hljs-comment"># Raises queue.Empty exception if empty.</span>
1778+
<pre><code class="python language-python hljs">&lt;bool&gt; = &lt;Future&gt;.done() <span class="hljs-comment"># Checks if the thread has finished executing.</span>
1779+
&lt;obj&gt; = &lt;Future&gt;.result(timeout=<span class="hljs-keyword">None</span>) <span class="hljs-comment"># Waits for thread to finish and returns result.</span>
1780+
&lt;bool&gt; = &lt;Future&gt;.cancel() <span class="hljs-comment"># Returns False if thread is already running.</span>
1781+
&lt;iter&gt; = as_completed(&lt;coll_of_Futures&gt;) <span class="hljs-comment"># Each Future is yielded as it completes.</span>
17881782
</code></pre>
1783+
<ul>
1784+
<li><strong>Map() and as_completed() also accept 'timeout' argument that causes TimeoutError if result isn't available in 'timeout' seconds after next() is called.</strong></li>
1785+
<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. It's exception() method returns exception or None.</strong></li>
1786+
<li><strong>An object with the same interface called ProcessPoolExecutor provides true parallelism by running a separate interpreter in each process. Arguments and results must be <a href="#pickle">pickable</a>.</strong></li>
1787+
</ul>
17891788
<div><h2 id="operator"><a href="#operator" name="operator">#</a>Operator</h2><p><strong>Module of functions that provide the functionality of operators.</strong></p><pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> operator <span class="hljs-keyword">as</span> op
17901789
&lt;obj&gt; = op.add/sub/mul/truediv/floordiv/mod(&lt;obj&gt;, &lt;obj&gt;) <span class="hljs-comment"># +, -, *, /, //, %</span>
17911790
&lt;int/set&gt; = op.and_/or_/xor(&lt;int/set&gt;, &lt;int/set&gt;) <span class="hljs-comment"># &amp;, |, ^</span>
@@ -2936,7 +2935,7 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment
29362935

29372936

29382937
<footer>
2939-
<aside>May 26, 2023</aside>
2938+
<aside>May 27, 2023</aside>
29402939
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
29412940
</footer>
29422941

0 commit comments

Comments
 (0)