Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f4f5fb1

Browse files
committed
Threading, Introspection
1 parent b4a54da commit f4f5fb1

File tree

2 files changed

+55
-51
lines changed

2 files changed

+55
-51
lines changed

README.md

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2050,19 +2050,19 @@ from threading import Thread, RLock, Semaphore, Event, Barrier
20502050

20512051
### Thread
20522052
```python
2053-
<Thread> = Thread(target=<function>) # Use `args=<collection>` to set arguments.
2054-
<Thread>.start() # Starts the thread.
2055-
<bool> = <Thread>.is_alive() # Checks if thread has finished executing.
2056-
<Thread>.join() # Waits for thread to finish.
2053+
<Thread> = Thread(target=<function>) # Use `args=<collection>` to set arguments.
2054+
<Thread>.start() # Starts the thread.
2055+
<bool> = <Thread>.is_alive() # Checks if thread has finished executing.
2056+
<Thread>.join() # Waits for thread to finish.
20572057
```
20582058
* **Use `'kwargs=<dict>'` to pass keyword arguments to the function.**
20592059
* **Use `'daemon=True'`, or the program will not be able to exit while the thread is alive.**
20602060

20612061
### Lock
20622062
```python
2063-
<lock> = RLock() # Lock that can only be released by the owner.
2064-
<lock>.acquire() # Waits for lock to be available.
2065-
<lock>.release() # Makes lock available again.
2063+
<lock> = RLock() # Lock that can only be released by the owner.
2064+
<lock>.acquire() # Waits for lock to be available.
2065+
<lock>.release() # Makes lock available again.
20662066
```
20672067

20682068
#### Or:
@@ -2074,24 +2074,27 @@ with lock:
20742074

20752075
### Semaphore, Event, Barrier
20762076
```python
2077-
<Semaphore> = Semaphore(value=1) # Lock that can be acquired by 'value' threads at once.
2078-
<Event> = Event() # Method wait() blocks until set() is called.
2079-
<Barrier> = Barrier(n_times) # Method wait() blocks until it's called 'n_times'.
2077+
<Semaphore> = Semaphore(value=1) # Lock that can be acquired by 'value' threads.
2078+
<Event> = Event() # Method wait() blocks until set() is called.
2079+
<Barrier> = Barrier(n_times) # Method wait() blocks until it's called n_times.
20802080
```
20812081

20822082
### Thread Pool Executor
2083+
**Object that manages thread execution.**
20832084
```python
20842085
from concurrent.futures import ThreadPoolExecutor
2085-
with ThreadPoolExecutor(max_workers=None) as executor: # Does not exit until done.
2086-
<iter> = executor.map(lambda x: x + 1, range(3)) # (1, 2, 3)
2087-
<iter> = executor.map(lambda x, y: x + y, 'abc', '123') # ('a1', 'b2', 'c3')
2088-
<Future> = executor.submit(<function> [, <arg_1>, ...]) # Also visible outside block.
20892086
```
20902087

2091-
#### Future:
20922088
```python
2093-
<bool> = <Future>.done() # Checks if thread has finished executing.
2094-
<obj> = <Future>.result() # Waits for thread to finish and returns result.
2089+
<Exec> = ThreadPoolExecutor([max_workers]) # Use max_workers to limit the number of threads.
2090+
<Exec>.shutdown(wait=True) # Or: `with ThreadPoolExecutor() as executor: …`
2091+
```
2092+
2093+
```python
2094+
<iter> = <Exec>.map(<func>, <args_1>, ...) # A multithreaded and non-lazy map().
2095+
<Futr> = <Exec>.submit(<func>, <arg_1>, ...) # Starts a thread and returns its Future object.
2096+
<bool> = <Futr>.done() # Checks if thread has finished executing.
2097+
<obj> = <Futr>.result() # Waits for thread to finish and returns result.
20952098
```
20962099

20972100
### Queue
@@ -2102,10 +2105,10 @@ from queue import Queue
21022105
```
21032106

21042107
```python
2105-
<Queue>.put(<el>) # Blocks until queue stops being full.
2106-
<Queue>.put_nowait(<el>) # Raises queue.Full exception if full.
2107-
<el> = <Queue>.get() # Blocks until queue stops being empty.
2108-
<el> = <Queue>.get_nowait() # Raises queue.Empty exception if empty.
2108+
<Queue>.put(<el>) # Blocks until queue stops being full.
2109+
<Queue>.put_nowait(<el>) # Raises queue.Full exception if full.
2110+
<el> = <Queue>.get() # Blocks until queue stops being empty.
2111+
<el> = <Queue>.get_nowait() # Raises queue.Empty exception if empty.
21092112
```
21102113

21112114

@@ -2155,10 +2158,10 @@ delattr(<object>, '<attr_name>') # Equivalent to `del <object>.<attr_n
21552158
### Parameters
21562159
```python
21572160
from inspect import signature
2158-
<sig> = signature(<function>)
2159-
no_of_params = len(<sig>.parameters)
2160-
param_names = list(<sig>.parameters.keys())
2161-
param_kinds = [a.kind for a in <sig>.parameters.values()]
2161+
<sig> = signature(<function>) # Signature object of the function.
2162+
<dict> = <sig>.parameters # Dict of function's parameters.
2163+
<str> = <param>.name # Prameter's name.
2164+
<memb> = <param>.kind # Member of ParameterKind enum.
21622165
```
21632166

21642167

index.html

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1835,51 +1835,52 @@
18351835
</code></pre></div>
18361836

18371837

1838-
<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>
1839-
&lt;Thread&gt;.start() <span class="hljs-comment"># Starts the thread.</span>
1840-
&lt;bool&gt; = &lt;Thread&gt;.is_alive() <span class="hljs-comment"># Checks if thread has finished executing.</span>
1841-
&lt;Thread&gt;.join() <span class="hljs-comment"># Waits for thread to finish.</span>
1838+
<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>
1839+
&lt;Thread&gt;.start() <span class="hljs-comment"># Starts the thread.</span>
1840+
&lt;bool&gt; = &lt;Thread&gt;.is_alive() <span class="hljs-comment"># Checks if thread has finished executing.</span>
1841+
&lt;Thread&gt;.join() <span class="hljs-comment"># Waits for thread to finish.</span>
18421842
</code></pre></div>
18431843

18441844
<ul>
18451845
<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>
18461846
<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>
18471847
</ul>
1848-
<div><h3 id="lock">Lock</h3><pre><code class="python language-python hljs">&lt;lock&gt; = RLock() <span class="hljs-comment"># Lock that can only be released by the owner.</span>
1849-
&lt;lock&gt;.acquire() <span class="hljs-comment"># Waits for lock to be available.</span>
1850-
&lt;lock&gt;.release() <span class="hljs-comment"># Makes lock available again.</span>
1848+
<div><h3 id="lock">Lock</h3><pre><code class="python language-python hljs">&lt;lock&gt; = RLock() <span class="hljs-comment"># Lock that can only be released by the owner.</span>
1849+
&lt;lock&gt;.acquire() <span class="hljs-comment"># Waits for lock to be available.</span>
1850+
&lt;lock&gt;.release() <span class="hljs-comment"># Makes lock available again.</span>
18511851
</code></pre></div>
18521852

18531853
<div><h4 id="or-1">Or:</h4><pre><code class="python language-python hljs">lock = RLock()
18541854
<span class="hljs-keyword">with</span> lock:
18551855
...
18561856
</code></pre></div>
18571857

1858-
<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 by 'value' threads at once.</span>
1859-
&lt;Event&gt; = Event() <span class="hljs-comment"># Method wait() blocks until set() is called.</span>
1860-
&lt;Barrier&gt; = Barrier(n_times) <span class="hljs-comment"># Method wait() blocks until it's called 'n_times'.</span>
1858+
<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 by 'value' threads.</span>
1859+
&lt;Event&gt; = Event() <span class="hljs-comment"># Method wait() blocks until set() is called.</span>
1860+
&lt;Barrier&gt; = Barrier(n_times) <span class="hljs-comment"># Method wait() blocks until it's called n_times.</span>
18611861
</code></pre></div>
18621862

1863-
<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
1864-
<span class="hljs-keyword">with</span> ThreadPoolExecutor(max_workers=<span class="hljs-keyword">None</span>) <span class="hljs-keyword">as</span> executor: <span class="hljs-comment"># Does not exit until done.</span>
1865-
&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>
1866-
&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>
1867-
&lt;Future&gt; = executor.submit(&lt;function&gt; [, &lt;arg_1&gt;, ...]) <span class="hljs-comment"># Also visible outside block.</span>
1863+
<div><h3 id="threadpoolexecutor">Thread Pool Executor</h3><p><strong>Object that manages thread execution.</strong></p><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> concurrent.futures <span class="hljs-keyword">import</span> ThreadPoolExecutor
18681864
</code></pre></div>
18691865

1870-
<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>
1871-
&lt;obj&gt; = &lt;Future&gt;.result() <span class="hljs-comment"># Waits for thread to finish and returns result.</span>
1872-
</code></pre></div>
18731866

1867+
<pre><code class="python language-python hljs">&lt;Exec&gt; = ThreadPoolExecutor([max_workers]) <span class="hljs-comment"># Use max_workers to limit the number of threads.</span>
1868+
&lt;Exec&gt;.shutdown(wait=<span class="hljs-keyword">True</span>) <span class="hljs-comment"># Or: `with ThreadPoolExecutor() as executor: …`</span>
1869+
</code></pre>
1870+
<pre><code class="python language-python hljs">&lt;iter&gt; = &lt;Exec&gt;.map(&lt;func&gt;, &lt;args_1&gt;, ...) <span class="hljs-comment"># A multithreaded and non-lazy map().</span>
1871+
&lt;Futr&gt; = &lt;Exec&gt;.submit(&lt;func&gt;, &lt;arg_1&gt;, ...) <span class="hljs-comment"># Starts a thread and returns its Future object.</span>
1872+
&lt;bool&gt; = &lt;Futr&gt;.done() <span class="hljs-comment"># Checks if thread has finished executing.</span>
1873+
&lt;obj&gt; = &lt;Futr&gt;.result() <span class="hljs-comment"># Waits for thread to finish and returns result.</span>
1874+
</code></pre>
18741875
<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
18751876
&lt;Queue&gt; = Queue(maxsize=<span class="hljs-number">0</span>)
18761877
</code></pre></div>
18771878

18781879

1879-
<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>
1880-
&lt;Queue&gt;.put_nowait(&lt;el&gt;) <span class="hljs-comment"># Raises queue.Full exception if full.</span>
1881-
&lt;el&gt; = &lt;Queue&gt;.get() <span class="hljs-comment"># Blocks until queue stops being empty.</span>
1882-
&lt;el&gt; = &lt;Queue&gt;.get_nowait() <span class="hljs-comment"># Raises queue.Empty exception if empty.</span>
1880+
<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>
1881+
&lt;Queue&gt;.put_nowait(&lt;el&gt;) <span class="hljs-comment"># Raises queue.Full exception if full.</span>
1882+
&lt;el&gt; = &lt;Queue&gt;.get() <span class="hljs-comment"># Blocks until queue stops being empty.</span>
1883+
&lt;el&gt; = &lt;Queue&gt;.get_nowait() <span class="hljs-comment"># Raises queue.Empty exception if empty.</span>
18831884
</code></pre>
18841885
<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
18851886
<span class="hljs-keyword">from</span> operator <span class="hljs-keyword">import</span> eq, ne, lt, le, gt, ge
@@ -1913,10 +1914,10 @@
19131914
</code></pre></div>
19141915

19151916
<div><h3 id="parameters-1">Parameters</h3><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> inspect <span class="hljs-keyword">import</span> signature
1916-
&lt;sig&gt; = signature(&lt;function&gt;)
1917-
no_of_params = len(&lt;sig&gt;.parameters)
1918-
param_names = list(&lt;sig&gt;.parameters.keys())
1919-
param_kinds = [a.kind <span class="hljs-keyword">for</span> a <span class="hljs-keyword">in</span> &lt;sig&gt;.parameters.values()]
1917+
&lt;sig&gt; = signature(&lt;function&gt;) <span class="hljs-comment"># Signature object of the function.</span>
1918+
&lt;dict&gt; = &lt;sig&gt;.parameters <span class="hljs-comment"># Dict of function's parameters.</span>
1919+
&lt;str&gt; = &lt;param&gt;.name <span class="hljs-comment"># Prameter's name.</span>
1920+
&lt;memb&gt; = &lt;param&gt;.kind <span class="hljs-comment"># Member of ParameterKind enum.</span>
19201921
</code></pre></div>
19211922

19221923
<div><h2 id="metaprograming"><a href="#metaprograming" name="metaprograming">#</a>Metaprograming</h2><p><strong>Code that generates code.</strong></p><div><h3 id="type-1">Type</h3><p><strong>Type is the root class. If only passed an object it returns its type (class). Otherwise it creates a new class.</strong></p><pre><code class="python language-python hljs">&lt;class&gt; = type(<span class="hljs-string">'&lt;class_name&gt;'</span>, &lt;parents_tuple&gt;, &lt;attributes_dict&gt;)</code></pre></div></div>

0 commit comments

Comments
 (0)