Skip to content

Commit 888c8a2

Browse files
committed
Threading
1 parent 39c91be commit 888c8a2

File tree

2 files changed

+32
-36
lines changed

2 files changed

+32
-36
lines changed

README.md

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2047,21 +2047,19 @@ from threading import Thread, RLock, Semaphore, Event, Barrier
20472047

20482048
### Thread
20492049
```python
2050-
thread = Thread(target=<function>, args=(<first_arg>, ))
2051-
thread.start()
2052-
...
2053-
<bool> = thread.is_alive() # Checks if thread has finished executing.
2054-
thread.join() # Waits for thread to finish.
2050+
<Thread> = Thread(target=<function>) # Use `args=<collection>` to set arguments.
2051+
<Thread>.start() # Starts the thread.
2052+
<bool> = <Thread>.is_alive() # Checks if thread has finished executing.
2053+
<Thread>.join() # Waits for thread to finish.
20552054
```
20562055
* **Use `'kwargs=<dict>'` to pass keyword arguments to the function.**
20572056
* **Use `'daemon=True'`, or the program will not be able to exit while the thread is alive.**
20582057

20592058
### Lock
20602059
```python
2061-
lock = RLock()
2062-
lock.acquire() # Waits for lock to be available.
2063-
...
2064-
lock.release()
2060+
<lock> = RLock()
2061+
<lock>.acquire() # Waits for lock to be available.
2062+
<lock>.release() # Makes the lock available again.
20652063
```
20662064

20672065
#### Or:
@@ -2073,9 +2071,9 @@ with lock:
20732071

20742072
### Semaphore, Event, Barrier
20752073
```python
2076-
<Semaphore> = Semaphore(value=1) # Lock that can be acquired 'value' times.
2077-
<Event> = Event() # Method wait() blocks until set() is called.
2078-
<Barrier> = Barrier(n_times) # Method wait() blocks until it's called 'n_times'.
2074+
<Semaphore> = Semaphore(value=1) # Lock that can be acquired 'value' times.
2075+
<Event> = Event() # Method wait() blocks until set() is called.
2076+
<Barrier> = Barrier(n_times) # Method wait() blocks until it's called 'n_times'.
20792077
```
20802078

20812079
### Thread Pool Executor
@@ -2089,8 +2087,8 @@ with ThreadPoolExecutor(max_workers=None) as executor: # Does not exit u
20892087

20902088
#### Future:
20912089
```python
2092-
<bool> = <Future>.done() # Checks if thread has finished executing.
2093-
<obj> = <Future>.result() # Waits for thread to finish and returns result.
2090+
<bool> = <Future>.done() # Checks if thread has finished executing.
2091+
<obj> = <Future>.result() # Waits for thread to finish and returns result.
20942092
```
20952093

20962094
### Queue
@@ -2101,10 +2099,10 @@ from queue import Queue
21012099
```
21022100

21032101
```python
2104-
<Queue>.put(<el>) # Blocks until queue stops being full.
2105-
<Queue>.put_nowait(<el>) # Raises queue.Full exception if full.
2106-
<el> = <Queue>.get() # Blocks until queue stops being empty.
2107-
<el> = <Queue>.get_nowait() # Raises queue.Empty exception if empty.
2102+
<Queue>.put(<el>) # Blocks until queue stops being full.
2103+
<Queue>.put_nowait(<el>) # Raises queue.Full exception if full.
2104+
<el> = <Queue>.get() # Blocks until queue stops being empty.
2105+
<el> = <Queue>.get_nowait() # Raises queue.Empty exception if empty.
21082106
```
21092107

21102108

index.html

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1805,31 +1805,29 @@
18051805
</code></pre></div>
18061806

18071807

1808-
<div><h3 id="thread">Thread</h3><pre><code class="python language-python hljs">thread = Thread(target=&lt;function&gt;, args=(&lt;first_arg&gt;, ))
1809-
thread.start()
1810-
...
1811-
&lt;bool&gt; = thread.is_alive() <span class="hljs-comment"># Checks if thread has finished executing.</span>
1812-
thread.join() <span class="hljs-comment"># Waits for thread to finish.</span>
1808+
<div><h3 id="thread">Thread</h3><pre><code class="python language-python hljs">&lt;Thread&gt; = Thread(target=&lt;function&gt;) <span class="hljs-comment"># Use `args=&lt;collection&gt;` to set arguments.</span>
1809+
&lt;Thread&gt;.start() <span class="hljs-comment"># Starts the thread.</span>
1810+
&lt;bool&gt; = &lt;Thread&gt;.is_alive() <span class="hljs-comment"># Checks if thread has finished executing.</span>
1811+
&lt;Thread&gt;.join() <span class="hljs-comment"># Waits for thread to finish.</span>
18131812
</code></pre></div>
18141813

18151814
<ul>
18161815
<li><strong>Use <code class="python hljs"><span class="hljs-string">'kwargs=&lt;dict&gt;'</span></code> to pass keyword arguments to the function.</strong></li>
18171816
<li><strong>Use <code class="python hljs"><span class="hljs-string">'daemon=True'</span></code>, or the program will not be able to exit while the thread is alive.</strong></li>
18181817
</ul>
1819-
<div><h3 id="lock">Lock</h3><pre><code class="python language-python hljs">lock = RLock()
1820-
lock.acquire() <span class="hljs-comment"># Waits for lock to be available.</span>
1821-
...
1822-
lock.release()
1818+
<div><h3 id="lock">Lock</h3><pre><code class="python language-python hljs">&lt;lock&gt; = RLock()
1819+
&lt;lock&gt;.acquire() <span class="hljs-comment"># Waits for lock to be available.</span>
1820+
&lt;lock&gt;.release() <span class="hljs-comment"># Makes the lock available again.</span>
18231821
</code></pre></div>
18241822

18251823
<div><h4 id="or-1">Or:</h4><pre><code class="python language-python hljs">lock = RLock()
18261824
<span class="hljs-keyword">with</span> lock:
18271825
...
18281826
</code></pre></div>
18291827

1830-
<div><h3 id="semaphoreeventbarrier">Semaphore, Event, Barrier</h3><pre><code class="python language-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>
1831-
&lt;Event&gt; = Event() <span class="hljs-comment"># Method wait() blocks until set() is called.</span>
1832-
&lt;Barrier&gt; = Barrier(n_times) <span class="hljs-comment"># Method wait() blocks until it's called 'n_times'.</span>
1828+
<div><h3 id="semaphoreeventbarrier">Semaphore, Event, Barrier</h3><pre><code class="python language-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>
1829+
&lt;Event&gt; = Event() <span class="hljs-comment"># Method wait() blocks until set() is called.</span>
1830+
&lt;Barrier&gt; = Barrier(n_times) <span class="hljs-comment"># Method wait() blocks until it's called 'n_times'.</span>
18331831
</code></pre></div>
18341832

18351833
<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
@@ -1839,19 +1837,19 @@
18391837
&lt;Future&gt; = executor.submit(&lt;function&gt; [, &lt;arg_1&gt;, ...]) <span class="hljs-comment"># Also visible outside block.</span>
18401838
</code></pre></div>
18411839

1842-
<div><h4 id="future">Future:</h4><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>
1843-
&lt;obj&gt; = &lt;Future&gt;.result() <span class="hljs-comment"># Waits for thread to finish and returns result.</span>
1840+
<div><h4 id="future">Future:</h4><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>
1841+
&lt;obj&gt; = &lt;Future&gt;.result() <span class="hljs-comment"># Waits for thread to finish and returns result.</span>
18441842
</code></pre></div>
18451843

18461844
<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
18471845
&lt;Queue&gt; = Queue(maxsize=<span class="hljs-number">0</span>)
18481846
</code></pre></div>
18491847

18501848

1851-
<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>
1852-
&lt;Queue&gt;.put_nowait(&lt;el&gt;) <span class="hljs-comment"># Raises queue.Full exception if full.</span>
1853-
&lt;el&gt; = &lt;Queue&gt;.get() <span class="hljs-comment"># Blocks until queue stops being empty.</span>
1854-
&lt;el&gt; = &lt;Queue&gt;.get_nowait() <span class="hljs-comment"># Raises queue.Empty exception if empty.</span>
1849+
<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>
1850+
&lt;Queue&gt;.put_nowait(&lt;el&gt;) <span class="hljs-comment"># Raises queue.Full exception if full.</span>
1851+
&lt;el&gt; = &lt;Queue&gt;.get() <span class="hljs-comment"># Blocks until queue stops being empty.</span>
1852+
&lt;el&gt; = &lt;Queue&gt;.get_nowait() <span class="hljs-comment"># Raises queue.Empty exception if empty.</span>
18551853
</code></pre>
18561854
<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">from</span> operator <span class="hljs-keyword">import</span> add, sub, mul, truediv, floordiv, mod, pow, neg, abs
18571855
<span class="hljs-keyword">from</span> operator <span class="hljs-keyword">import</span> eq, ne, lt, le, gt, ge

0 commit comments

Comments
 (0)