|
1729 | 1729 | IndexError: deque already at its maximum size
|
1730 | 1730 | </code></pre></div>
|
1731 | 1731 |
|
1732 |
| -<div><h2 id="threading"><a href="#threading" name="threading">#</a>Threading</h2><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> threading <span class="hljs-keyword">import</span> Thread, RLock |
| 1732 | +<div><h2 id="threading"><a href="#threading" name="threading">#</a>Threading</h2><ul> |
| 1733 | +<li><strong>CPython interpreter can only run a single thread at the time.</strong></li> |
| 1734 | +<li><strong>That is why using multiple threads won't result in a faster execution, unless there is an I/O operation in the thread.</strong></li> |
| 1735 | +</ul><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> threading <span class="hljs-keyword">import</span> Thread, RLock |
1733 | 1736 | </code></pre></div>
|
1734 | 1737 |
|
| 1738 | + |
1735 | 1739 | <div><h3 id="thread">Thread</h3><pre><code class="python language-python hljs">thread = Thread(target=<function>, args=(<first_arg>, ))
|
1736 | 1740 | thread.start()
|
1737 | 1741 | ...
|
|
1751 | 1755 |
|
1752 | 1756 | <div><h3 id="threadpool">Thread Pool</h3><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> concurrent.futures <span class="hljs-keyword">import</span> ThreadPoolExecutor
|
1753 | 1757 | <span class="hljs-keyword">with</span> ThreadPoolExecutor(max_workers=<span class="hljs-keyword">None</span>) <span class="hljs-keyword">as</span> executor:
|
1754 |
| - results = executor.map(<span class="hljs-keyword">lambda</span> x: x + <span class="hljs-number">1</span>, range(<span class="hljs-number">3</span>)) <span class="hljs-comment"># (1, 2, 3)</span> |
1755 |
| - results = executor.map(<span class="hljs-keyword">lambda</span> x, y: x + y, <span class="hljs-string">'abc'</span>, <span class="hljs-string">'123'</span>) <span class="hljs-comment"># ('a1', 'b2', 'c3')</span> |
| 1758 | + <iter> = executor.map(<span class="hljs-keyword">lambda</span> x: x + <span class="hljs-number">1</span>, range(<span class="hljs-number">3</span>)) <span class="hljs-comment"># (1, 2, 3)</span> |
| 1759 | + <iter> = executor.map(<span class="hljs-keyword">lambda</span> x, y: x + y, <span class="hljs-string">'abc'</span>, <span class="hljs-string">'123'</span>) <span class="hljs-comment"># ('a1', 'b2', 'c3')</span> |
| 1760 | + <Future> = executor.submit(<function>, <arg_1>, ...) |
1756 | 1761 | </code></pre></div>
|
1757 | 1762 |
|
1758 |
| -<ul> |
1759 |
| -<li><strong>CPython interpreter can only run a single thread at the time. That is why this map() won't be faster than the standard map(), unless passed function contains an I/O operation.</strong></li> |
1760 |
| -</ul> |
| 1763 | +<pre><code class="python language-python hljs"><bool> = <Future>.done() <span class="hljs-comment"># Checks if thread has finished executing.</span> |
| 1764 | +<obj> = <Future>.result() <span class="hljs-comment"># Waits for thread to finish and returns result.</span> |
| 1765 | +</code></pre> |
1761 | 1766 | <div><h2 id="operator"><a href="#operator" name="operator">#</a>Operator</h2><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> operator <span class="hljs-keyword">import</span> add, sub, mul, truediv, floordiv, mod, pow, neg, abs
|
1762 | 1767 | <span class="hljs-keyword">from</span> operator <span class="hljs-keyword">import</span> eq, ne, lt, le, gt, ge
|
1763 | 1768 | <span class="hljs-keyword">from</span> operator <span class="hljs-keyword">import</span> and_, or_, not_
|
|
0 commit comments