|
1805 | 1805 | </code></pre></div>
|
1806 | 1806 |
|
1807 | 1807 |
|
1808 |
| -<div><h3 id="thread">Thread</h3><pre><code class="python language-python hljs">thread = Thread(target=<function>, args=(<first_arg>, )) |
1809 |
| -thread.start() |
1810 |
| -... |
1811 |
| -<bool> = 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"><Thread> = Thread(target=<function>) <span class="hljs-comment"># Use `args=<collection>` to set arguments.</span> |
| 1809 | +<Thread>.start() <span class="hljs-comment"># Starts the thread.</span> |
| 1810 | +<bool> = <Thread>.is_alive() <span class="hljs-comment"># Checks if thread has finished executing.</span> |
| 1811 | +<Thread>.join() <span class="hljs-comment"># Waits for thread to finish.</span> |
1813 | 1812 | </code></pre></div>
|
1814 | 1813 |
|
1815 | 1814 | <ul>
|
1816 | 1815 | <li><strong>Use <code class="python hljs"><span class="hljs-string">'kwargs=<dict>'</span></code> to pass keyword arguments to the function.</strong></li>
|
1817 | 1816 | <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>
|
1818 | 1817 | </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"><lock> = RLock() |
| 1819 | +<lock>.acquire() <span class="hljs-comment"># Waits for lock to be available.</span> |
| 1820 | +<lock>.release() <span class="hljs-comment"># Makes the lock available again.</span> |
1823 | 1821 | </code></pre></div>
|
1824 | 1822 |
|
1825 | 1823 | <div><h4 id="or-1">Or:</h4><pre><code class="python language-python hljs">lock = RLock()
|
1826 | 1824 | <span class="hljs-keyword">with</span> lock:
|
1827 | 1825 | ...
|
1828 | 1826 | </code></pre></div>
|
1829 | 1827 |
|
1830 |
| -<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 'value' times.</span> |
1831 |
| -<Event> = Event() <span class="hljs-comment"># Method wait() blocks until set() is called.</span> |
1832 |
| -<Barrier> = 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"><Semaphore> = Semaphore(value=<span class="hljs-number">1</span>) <span class="hljs-comment"># Lock that can be acquired 'value' times.</span> |
| 1829 | +<Event> = Event() <span class="hljs-comment"># Method wait() blocks until set() is called.</span> |
| 1830 | +<Barrier> = Barrier(n_times) <span class="hljs-comment"># Method wait() blocks until it's called 'n_times'.</span> |
1833 | 1831 | </code></pre></div>
|
1834 | 1832 |
|
1835 | 1833 | <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 | 1837 | <Future> = executor.submit(<function> [, <arg_1>, ...]) <span class="hljs-comment"># Also visible outside block.</span>
|
1840 | 1838 | </code></pre></div>
|
1841 | 1839 |
|
1842 |
| -<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> |
1843 |
| -<obj> = <Future>.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"><bool> = <Future>.done() <span class="hljs-comment"># Checks if thread has finished executing.</span> |
| 1841 | +<obj> = <Future>.result() <span class="hljs-comment"># Waits for thread to finish and returns result.</span> |
1844 | 1842 | </code></pre></div>
|
1845 | 1843 |
|
1846 | 1844 | <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
|
1847 | 1845 | <Queue> = Queue(maxsize=<span class="hljs-number">0</span>)
|
1848 | 1846 | </code></pre></div>
|
1849 | 1847 |
|
1850 | 1848 |
|
1851 |
| -<pre><code class="python language-python hljs"><Queue>.put(<el>) <span class="hljs-comment"># Blocks until queue stops being full.</span> |
1852 |
| -<Queue>.put_nowait(<el>) <span class="hljs-comment"># Raises queue.Full exception if full.</span> |
1853 |
| -<el> = <Queue>.get() <span class="hljs-comment"># Blocks until queue stops being empty.</span> |
1854 |
| -<el> = <Queue>.get_nowait() <span class="hljs-comment"># Raises queue.Empty exception if empty.</span> |
| 1849 | +<pre><code class="python language-python hljs"><Queue>.put(<el>) <span class="hljs-comment"># Blocks until queue stops being full.</span> |
| 1850 | +<Queue>.put_nowait(<el>) <span class="hljs-comment"># Raises queue.Full exception if full.</span> |
| 1851 | +<el> = <Queue>.get() <span class="hljs-comment"># Blocks until queue stops being empty.</span> |
| 1852 | +<el> = <Queue>.get_nowait() <span class="hljs-comment"># Raises queue.Empty exception if empty.</span> |
1855 | 1853 | </code></pre>
|
1856 | 1854 | <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
|
1857 | 1855 | <span class="hljs-keyword">from</span> operator <span class="hljs-keyword">import</span> eq, ne, lt, le, gt, ge
|
|
0 commit comments