|
1835 | 1835 | </code></pre></div>
|
1836 | 1836 |
|
1837 | 1837 |
|
1838 |
| -<div><h3 id="thread">Thread</h3><pre><code class="python language-python hljs"><Thread> = Thread(target=<function>) <span class="hljs-comment"># Use `args=<collection>` to set arguments.</span> |
1839 |
| -<Thread>.start() <span class="hljs-comment"># Starts the thread.</span> |
1840 |
| -<bool> = <Thread>.is_alive() <span class="hljs-comment"># Checks if thread has finished executing.</span> |
1841 |
| -<Thread>.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"><Thread> = Thread(target=<function>) <span class="hljs-comment"># Use `args=<collection>` to set arguments.</span> |
| 1839 | +<Thread>.start() <span class="hljs-comment"># Starts the thread.</span> |
| 1840 | +<bool> = <Thread>.is_alive() <span class="hljs-comment"># Checks if thread has finished executing.</span> |
| 1841 | +<Thread>.join() <span class="hljs-comment"># Waits for thread to finish.</span> |
1842 | 1842 | </code></pre></div>
|
1843 | 1843 |
|
1844 | 1844 | <ul>
|
1845 | 1845 | <li><strong>Use <code class="python hljs"><span class="hljs-string">'kwargs=<dict>'</span></code> to pass keyword arguments to the function.</strong></li>
|
1846 | 1846 | <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>
|
1847 | 1847 | </ul>
|
1848 |
| -<div><h3 id="lock">Lock</h3><pre><code class="python language-python hljs"><lock> = RLock() <span class="hljs-comment"># Lock that can only be released by the owner.</span> |
1849 |
| -<lock>.acquire() <span class="hljs-comment"># Waits for lock to be available.</span> |
1850 |
| -<lock>.release() <span class="hljs-comment"># Makes lock available again.</span> |
| 1848 | +<div><h3 id="lock">Lock</h3><pre><code class="python language-python hljs"><lock> = RLock() <span class="hljs-comment"># Lock that can only be released by the owner.</span> |
| 1849 | +<lock>.acquire() <span class="hljs-comment"># Waits for lock to be available.</span> |
| 1850 | +<lock>.release() <span class="hljs-comment"># Makes lock available again.</span> |
1851 | 1851 | </code></pre></div>
|
1852 | 1852 |
|
1853 | 1853 | <div><h4 id="or-1">Or:</h4><pre><code class="python language-python hljs">lock = RLock()
|
1854 | 1854 | <span class="hljs-keyword">with</span> lock:
|
1855 | 1855 | ...
|
1856 | 1856 | </code></pre></div>
|
1857 | 1857 |
|
1858 |
| -<div><h3 id="semaphoreeventbarrier">Semaphore, Event, Barrier</h3><pre><code class="python language-python hljs"><Semaphore> = Semaphore(value=<span class="hljs-number">1</span>) <span class="hljs-comment"># Lock that can be acquired by 'value' threads at once.</span> |
1859 |
| -<Event> = Event() <span class="hljs-comment"># Method wait() blocks until set() is called.</span> |
1860 |
| -<Barrier> = 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"><Semaphore> = Semaphore(value=<span class="hljs-number">1</span>) <span class="hljs-comment"># Lock that can be acquired by 'value' threads.</span> |
| 1859 | +<Event> = Event() <span class="hljs-comment"># Method wait() blocks until set() is called.</span> |
| 1860 | +<Barrier> = Barrier(n_times) <span class="hljs-comment"># Method wait() blocks until it's called n_times.</span> |
1861 | 1861 | </code></pre></div>
|
1862 | 1862 |
|
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 |
| - <iter> = 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 |
| - <iter> = 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 |
| - <Future> = executor.submit(<function> [, <arg_1>, ...]) <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 |
1868 | 1864 | </code></pre></div>
|
1869 | 1865 |
|
1870 |
| -<div><h4 id="future">Future:</h4><pre><code class="python language-python hljs"><bool> = <Future>.done() <span class="hljs-comment"># Checks if thread has finished executing.</span> |
1871 |
| -<obj> = <Future>.result() <span class="hljs-comment"># Waits for thread to finish and returns result.</span> |
1872 |
| -</code></pre></div> |
1873 | 1866 |
|
| 1867 | +<pre><code class="python language-python hljs"><Exec> = ThreadPoolExecutor([max_workers]) <span class="hljs-comment"># Use max_workers to limit the number of threads.</span> |
| 1868 | +<Exec>.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"><iter> = <Exec>.map(<func>, <args_1>, ...) <span class="hljs-comment"># A multithreaded and non-lazy map().</span> |
| 1871 | +<Futr> = <Exec>.submit(<func>, <arg_1>, ...) <span class="hljs-comment"># Starts a thread and returns its Future object.</span> |
| 1872 | +<bool> = <Futr>.done() <span class="hljs-comment"># Checks if thread has finished executing.</span> |
| 1873 | +<obj> = <Futr>.result() <span class="hljs-comment"># Waits for thread to finish and returns result.</span> |
| 1874 | +</code></pre> |
1874 | 1875 | <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
|
1875 | 1876 | <Queue> = Queue(maxsize=<span class="hljs-number">0</span>)
|
1876 | 1877 | </code></pre></div>
|
1877 | 1878 |
|
1878 | 1879 |
|
1879 |
| -<pre><code class="python language-python hljs"><Queue>.put(<el>) <span class="hljs-comment"># Blocks until queue stops being full.</span> |
1880 |
| -<Queue>.put_nowait(<el>) <span class="hljs-comment"># Raises queue.Full exception if full.</span> |
1881 |
| -<el> = <Queue>.get() <span class="hljs-comment"># Blocks until queue stops being empty.</span> |
1882 |
| -<el> = <Queue>.get_nowait() <span class="hljs-comment"># Raises queue.Empty exception if empty.</span> |
| 1880 | +<pre><code class="python language-python hljs"><Queue>.put(<el>) <span class="hljs-comment"># Blocks until queue stops being full.</span> |
| 1881 | +<Queue>.put_nowait(<el>) <span class="hljs-comment"># Raises queue.Full exception if full.</span> |
| 1882 | +<el> = <Queue>.get() <span class="hljs-comment"># Blocks until queue stops being empty.</span> |
| 1883 | +<el> = <Queue>.get_nowait() <span class="hljs-comment"># Raises queue.Empty exception if empty.</span> |
1883 | 1884 | </code></pre>
|
1884 | 1885 | <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
|
1885 | 1886 | <span class="hljs-keyword">from</span> operator <span class="hljs-keyword">import</span> eq, ne, lt, le, gt, ge
|
|
1913 | 1914 | </code></pre></div>
|
1914 | 1915 |
|
1915 | 1916 | <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 |
| -<sig> = signature(<function>) |
1917 |
| -no_of_params = len(<sig>.parameters) |
1918 |
| -param_names = list(<sig>.parameters.keys()) |
1919 |
| -param_kinds = [a.kind <span class="hljs-keyword">for</span> a <span class="hljs-keyword">in</span> <sig>.parameters.values()] |
| 1917 | +<sig> = signature(<function>) <span class="hljs-comment"># Signature object of the function.</span> |
| 1918 | +<dict> = <sig>.parameters <span class="hljs-comment"># Dict of function's parameters.</span> |
| 1919 | +<str> = <param>.name <span class="hljs-comment"># Prameter's name.</span> |
| 1920 | +<memb> = <param>.kind <span class="hljs-comment"># Member of ParameterKind enum.</span> |
1920 | 1921 | </code></pre></div>
|
1921 | 1922 |
|
1922 | 1923 | <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"><class> = type(<span class="hljs-string">'<class_name>'</span>, <parents_tuple>, <attributes_dict>)</code></pre></div></div>
|
|
0 commit comments