|
54 | 54 |
|
55 | 55 | <body>
|
56 | 56 | <header>
|
57 |
| - <aside>May 26, 2023</aside> |
| 57 | + <aside>May 27, 2023</aside> |
58 | 58 | <a href="https://gto76.github.io" rel="author">Jure Šorn</a>
|
59 | 59 | </header>
|
60 | 60 |
|
@@ -1762,30 +1762,29 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment
|
1762 | 1762 | <Barrier> = Barrier(n_times) <span class="hljs-comment"># Wait() blocks until it's called n_times.</span>
|
1763 | 1763 | </code></pre></div>
|
1764 | 1764 |
|
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"><Exec> = ThreadPoolExecutor(max_workers=<span class="hljs-keyword">None</span>) <span class="hljs-comment"># Or: `with ThreadPoolExecutor() as <name>: …`</span> |
1769 |
| -<Exec>.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"><Queue> = queue.Queue(maxsize=<span class="hljs-number">0</span>) <span class="hljs-comment"># A thread-safe FIFO queue. Also LifoQueue.</span> |
| 1766 | +<Queue>.put(<el>) <span class="hljs-comment"># Blocks until queue stops being full.</span> |
| 1767 | +<Queue>.put_nowait(<el>) <span class="hljs-comment"># Raises queue.Full exception if full.</span> |
| 1768 | +<el> = <Queue>.get() <span class="hljs-comment"># Blocks until queue stops being empty.</span> |
| 1769 | +<el> = <Queue>.get_nowait() <span class="hljs-comment"># Raises queue.Empty exception if empty.</span> |
1770 | 1770 | </code></pre></div>
|
1771 | 1771 |
|
1772 |
| - |
1773 |
| -<pre><code class="python language-python hljs"><iter> = <Exec>.map(<func>, <args_1>, ...) <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"><Exec> = ThreadPoolExecutor(max_workers=<span class="hljs-keyword">None</span>) <span class="hljs-comment"># Or: `with ThreadPoolExecutor() as <name>: …`</span> |
| 1773 | +<iter> = <Exec>.map(<func>, <args_1>, ...) <span class="hljs-comment"># A multithreaded non-lazy map(). Keeps order.</span> |
1774 | 1774 | <Futr> = <Exec>.submit(<func>, <arg_1>, ...) <span class="hljs-comment"># Starts a thread and returns its Future object.</span>
|
1775 |
| -<bool> = <Futr>.done() <span class="hljs-comment"># Checks if the thread has finished executing.</span> |
1776 |
| -<obj> = <Futr>.result() <span class="hljs-comment"># Waits for thread to finish and returns result.</span> |
1777 |
| -<iter> = as_completed(<coll_of_Futr>) <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 |
| -<Queue> = Queue(maxsize=<span class="hljs-number">0</span>) |
| 1775 | +<Exec>.shutdown(wait=<span class="hljs-keyword">True</span>) <span class="hljs-comment"># Blocks until all threads finish executing.</span> |
1781 | 1776 | </code></pre></div>
|
1782 | 1777 |
|
1783 |
| - |
1784 |
| -<pre><code class="python language-python hljs"><Queue>.put(<el>) <span class="hljs-comment"># Blocks until queue stops being full.</span> |
1785 |
| -<Queue>.put_nowait(<el>) <span class="hljs-comment"># Raises queue.Full exception if full.</span> |
1786 |
| -<el> = <Queue>.get() <span class="hljs-comment"># Blocks until queue stops being empty.</span> |
1787 |
| -<el> = <Queue>.get_nowait() <span class="hljs-comment"># Raises queue.Empty exception if empty.</span> |
| 1778 | +<pre><code class="python language-python hljs"><bool> = <Future>.done() <span class="hljs-comment"># Checks if the thread has finished executing.</span> |
| 1779 | +<obj> = <Future>.result(timeout=<span class="hljs-keyword">None</span>) <span class="hljs-comment"># Waits for thread to finish and returns result.</span> |
| 1780 | +<bool> = <Future>.cancel() <span class="hljs-comment"># Returns False if thread is already running.</span> |
| 1781 | +<iter> = as_completed(<coll_of_Futures>) <span class="hljs-comment"># Each Future is yielded as it completes.</span> |
1788 | 1782 | </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> |
1789 | 1788 | <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
|
1790 | 1789 | <obj> = op.add/sub/mul/truediv/floordiv/mod(<obj>, <obj>) <span class="hljs-comment"># +, -, *, /, //, %</span>
|
1791 | 1790 | <int/set> = op.and_/or_/xor(<int/set>, <int/set>) <span class="hljs-comment"># &, |, ^</span>
|
@@ -2936,7 +2935,7 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment
|
2936 | 2935 |
|
2937 | 2936 |
|
2938 | 2937 | <footer>
|
2939 |
| - <aside>May 26, 2023</aside> |
| 2938 | + <aside>May 27, 2023</aside> |
2940 | 2939 | <a href="https://gto76.github.io" rel="author">Jure Šorn</a>
|
2941 | 2940 | </footer>
|
2942 | 2941 |
|
|
0 commit comments