Skip to content

Commit 5538b76

Browse files
committed
Threading
1 parent a243795 commit 5538b76

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

README.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1962,6 +1962,8 @@ IndexError: deque already at its maximum size
19621962

19631963
Threading
19641964
---------
1965+
* **CPython interpreter can only run a single thread at the time.**
1966+
* **That is why using multiple threads won't result in a faster execution, unless there is an I/O operation in the thread.**
19651967
```python
19661968
from threading import Thread, RLock
19671969
```
@@ -1993,10 +1995,15 @@ with lock:
19931995
```python
19941996
from concurrent.futures import ThreadPoolExecutor
19951997
with ThreadPoolExecutor(max_workers=None) as executor:
1996-
results = executor.map(lambda x: x + 1, range(3)) # (1, 2, 3)
1997-
results = executor.map(lambda x, y: x + y, 'abc', '123') # ('a1', 'b2', 'c3')
1998+
<iter> = executor.map(lambda x: x + 1, range(3)) # (1, 2, 3)
1999+
<iter> = executor.map(lambda x, y: x + y, 'abc', '123') # ('a1', 'b2', 'c3')
2000+
<Future> = executor.submit(<function>, <arg_1>, ...)
2001+
```
2002+
2003+
```python
2004+
<bool> = <Future>.done() # Checks if thread has finished executing.
2005+
<obj> = <Future>.result() # Waits for thread to finish and returns result.
19982006
```
1999-
* **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.**
20002007

20012008

20022009
Operator

index.html

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1729,9 +1729,13 @@
17291729
IndexError: deque already at its maximum size
17301730
</code></pre></div>
17311731

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
17331736
</code></pre></div>
17341737

1738+
17351739
<div><h3 id="thread">Thread</h3><pre><code class="python language-python hljs">thread = Thread(target=&lt;function&gt;, args=(&lt;first_arg&gt;, ))
17361740
thread.start()
17371741
...
@@ -1751,13 +1755,14 @@
17511755

17521756
<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
17531757
<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+
&lt;iter&gt; = 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+
&lt;iter&gt; = 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+
&lt;Future&gt; = executor.submit(&lt;function&gt;, &lt;arg_1&gt;, ...)
17561761
</code></pre></div>
17571762

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">&lt;bool&gt; = &lt;Future&gt;.done() <span class="hljs-comment"># Checks if thread has finished executing.</span>
1764+
&lt;obj&gt; = &lt;Future&gt;.result() <span class="hljs-comment"># Waits for thread to finish and returns result.</span>
1765+
</code></pre>
17611766
<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
17621767
<span class="hljs-keyword">from</span> operator <span class="hljs-keyword">import</span> eq, ne, lt, le, gt, ge
17631768
<span class="hljs-keyword">from</span> operator <span class="hljs-keyword">import</span> and_, or_, not_

0 commit comments

Comments
 (0)