Skip to content

Commit 9ac6049

Browse files
committed
Threading
1 parent df51880 commit 9ac6049

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2034,7 +2034,7 @@ Threading
20342034
* **CPython interpreter can only run a single thread at a time.**
20352035
* **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.**
20362036
```python
2037-
from threading import Thread, RLock
2037+
from threading import Thread, RLock, Semaphore, Event, Barrier
20382038
```
20392039

20402040
### Thread
@@ -2063,6 +2063,13 @@ with lock:
20632063
...
20642064
```
20652065

2066+
### Semaphore, Event, Barrier
2067+
```
2068+
<Semaphore> = Semaphore(value=1) # Lock that can be acquired 'value' times.
2069+
<Event> = Event() # Method wait() blocks until set() is called.
2070+
<Barrier> = Barrier(n_times) # Method wait() blocks until it's called n_times.
2071+
```
2072+
20662073
### Thread Pool Executor
20672074
```python
20682075
from concurrent.futures import ThreadPoolExecutor

index.html

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1793,7 +1793,7 @@
17931793
<div><h2 id="threading"><a href="#threading" name="threading">#</a>Threading</h2><ul>
17941794
<li><strong>CPython interpreter can only run a single thread at a time.</strong></li>
17951795
<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>
1796-
</ul><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> threading <span class="hljs-keyword">import</span> Thread, RLock
1796+
</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
17971797
</code></pre></div>
17981798

17991799

@@ -1819,6 +1819,11 @@
18191819
...
18201820
</code></pre></div>
18211821

1822+
<div><h3 id="semaphoreeventbarrier">Semaphore, Event, Barrier</h3><pre><code class="python hljs">&lt;Semaphore&gt; = Semaphore(value=<span class="hljs-number">1</span>) <span class="hljs-comment"># Lock that can be acquired 'value' times.</span>
1823+
&lt;Event&gt; = Event() <span class="hljs-comment"># Method wait() blocks until set() is called.</span>
1824+
&lt;Barrier&gt; = Barrier(n_times) <span class="hljs-comment"># Method wait() blocks until it's called n_times.</span>
1825+
</code></pre></div>
1826+
18221827
<div><h3 id="threadpoolexecutor">Thread Pool Executor</h3><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> concurrent.futures <span class="hljs-keyword">import</span> ThreadPoolExecutor
18231828
<span class="hljs-keyword">with</span> ThreadPoolExecutor(max_workers=<span class="hljs-keyword">None</span>) <span class="hljs-keyword">as</span> executor:
18241829
&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>

0 commit comments

Comments
 (0)