Browse Source

Threading

pull/57/head
Jure Šorn 4 years ago
parent
commit
888c8a29c1
2 changed files with 32 additions and 36 deletions
  1. 34
      README.md
  2. 34
      index.html

34
README.md

@ -2047,21 +2047,19 @@ from threading import Thread, RLock, Semaphore, Event, Barrier
### Thread
```python
thread = Thread(target=<function>, args=(<first_arg>, ))
thread.start()
...
<bool> = thread.is_alive() # Checks if thread has finished executing.
thread.join() # Waits for thread to finish.
<Thread> = Thread(target=<function>) # Use `args=<collection>` to set arguments.
<Thread>.start() # Starts the thread.
<bool> = <Thread>.is_alive() # Checks if thread has finished executing.
<Thread>.join() # Waits for thread to finish.
```
* **Use `'kwargs=<dict>'` to pass keyword arguments to the function.**
* **Use `'daemon=True'`, or the program will not be able to exit while the thread is alive.**
### Lock
```python
lock = RLock()
lock.acquire() # Waits for lock to be available.
...
lock.release()
<lock> = RLock()
<lock>.acquire() # Waits for lock to be available.
<lock>.release() # Makes the lock available again.
```
#### Or:
@ -2073,9 +2071,9 @@ with lock:
### Semaphore, Event, Barrier
```python
<Semaphore> = Semaphore(value=1) # Lock that can be acquired 'value' times.
<Event> = Event() # Method wait() blocks until set() is called.
<Barrier> = Barrier(n_times) # Method wait() blocks until it's called 'n_times'.
<Semaphore> = Semaphore(value=1) # Lock that can be acquired 'value' times.
<Event> = Event() # Method wait() blocks until set() is called.
<Barrier> = Barrier(n_times) # Method wait() blocks until it's called 'n_times'.
```
### Thread Pool Executor
@ -2089,8 +2087,8 @@ with ThreadPoolExecutor(max_workers=None) as executor: # Does not exit u
#### Future:
```python
<bool> = <Future>.done() # Checks if thread has finished executing.
<obj> = <Future>.result() # Waits for thread to finish and returns result.
<bool> = <Future>.done() # Checks if thread has finished executing.
<obj> = <Future>.result() # Waits for thread to finish and returns result.
```
### Queue
@ -2101,10 +2099,10 @@ from queue import Queue
```
```python
<Queue>.put(<el>) # Blocks until queue stops being full.
<Queue>.put_nowait(<el>) # Raises queue.Full exception if full.
<el> = <Queue>.get() # Blocks until queue stops being empty.
<el> = <Queue>.get_nowait() # Raises queue.Empty exception if empty.
<Queue>.put(<el>) # Blocks until queue stops being full.
<Queue>.put_nowait(<el>) # Raises queue.Full exception if full.
<el> = <Queue>.get() # Blocks until queue stops being empty.
<el> = <Queue>.get_nowait() # Raises queue.Empty exception if empty.
```

34
index.html

@ -1805,21 +1805,19 @@ CompletedProcess(args=[<span class="hljs-string">'bc'</span>, <span class="hljs-
</code></pre></div>
<div><h3 id="thread">Thread</h3><pre><code class="python language-python hljs">thread = Thread(target=&lt;function&gt;, args=(&lt;first_arg&gt;, ))
thread.start()
...
&lt;bool&gt; = thread.is_alive() <span class="hljs-comment"># Checks if thread has finished executing.</span>
thread.join() <span class="hljs-comment"># Waits for thread to finish.</span>
<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>
&lt;Thread&gt;.start() <span class="hljs-comment"># Starts the thread.</span>
&lt;bool&gt; = &lt;Thread&gt;.is_alive() <span class="hljs-comment"># Checks if thread has finished executing.</span>
&lt;Thread&gt;.join() <span class="hljs-comment"># Waits for thread to finish.</span>
</code></pre></div>
<ul>
<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>
<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>
</ul>
<div><h3 id="lock">Lock</h3><pre><code class="python language-python hljs">lock = RLock()
lock.acquire() <span class="hljs-comment"># Waits for lock to be available.</span>
...
lock.release()
<div><h3 id="lock">Lock</h3><pre><code class="python language-python hljs">&lt;lock&gt; = RLock()
&lt;lock&gt;.acquire() <span class="hljs-comment"># Waits for lock to be available.</span>
&lt;lock&gt;.release() <span class="hljs-comment"># Makes the lock available again.</span>
</code></pre></div>
<div><h4 id="or-1">Or:</h4><pre><code class="python language-python hljs">lock = RLock()
@ -1827,9 +1825,9 @@ lock.release()
...
</code></pre></div>
<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>
&lt;Event&gt; = Event() <span class="hljs-comment"># Method wait() blocks until set() is called.</span>
&lt;Barrier&gt; = Barrier(n_times) <span class="hljs-comment"># Method wait() blocks until it's called 'n_times'.</span>
<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>
&lt;Event&gt; = Event() <span class="hljs-comment"># Method wait() blocks until set() is called.</span>
&lt;Barrier&gt; = Barrier(n_times) <span class="hljs-comment"># Method wait() blocks until it's called 'n_times'.</span>
</code></pre></div>
<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,8 +1837,8 @@ lock.release()
&lt;Future&gt; = executor.submit(&lt;function&gt; [, &lt;arg_1&gt;, ...]) <span class="hljs-comment"># Also visible outside block.</span>
</code></pre></div>
<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>
&lt;obj&gt; = &lt;Future&gt;.result() <span class="hljs-comment"># Waits for thread to finish and returns result.</span>
<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>
&lt;obj&gt; = &lt;Future&gt;.result() <span class="hljs-comment"># Waits for thread to finish and returns result.</span>
</code></pre></div>
<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
@ -1848,10 +1846,10 @@ lock.release()
</code></pre></div>
<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>
&lt;Queue&gt;.put_nowait(&lt;el&gt;) <span class="hljs-comment"># Raises queue.Full exception if full.</span>
&lt;el&gt; = &lt;Queue&gt;.get() <span class="hljs-comment"># Blocks until queue stops being empty.</span>
&lt;el&gt; = &lt;Queue&gt;.get_nowait() <span class="hljs-comment"># Raises queue.Empty exception if empty.</span>
<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>
&lt;Queue&gt;.put_nowait(&lt;el&gt;) <span class="hljs-comment"># Raises queue.Full exception if full.</span>
&lt;el&gt; = &lt;Queue&gt;.get() <span class="hljs-comment"># Blocks until queue stops being empty.</span>
&lt;el&gt; = &lt;Queue&gt;.get_nowait() <span class="hljs-comment"># Raises queue.Empty exception if empty.</span>
</code></pre>
<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
<span class="hljs-keyword">from</span> operator <span class="hljs-keyword">import</span> eq, ne, lt, le, gt, ge

Loading…
Cancel
Save