<div><h3id="thread">Thread</h3><pre><codeclass="python language-python hljs"><Thread> = Thread(target=<function>) <spanclass="hljs-comment"># Use `args=<collection>` to set arguments.</span>
<Thread>.start() <spanclass="hljs-comment"># Starts the thread.</span>
<bool> = <Thread>.is_alive() <spanclass="hljs-comment"># Checks if thread has finished executing.</span>
<Thread>.join() <spanclass="hljs-comment"># Waits for thread to finish.</span>
<div><h3id="thread">Thread</h3><pre><codeclass="python language-python hljs"><Thread> = Thread(target=<function>) <spanclass="hljs-comment"># Use `args=<collection>` to set arguments.</span>
<Thread>.start() <spanclass="hljs-comment"># Starts the thread.</span>
<bool> = <Thread>.is_alive() <spanclass="hljs-comment"># Checks if thread has finished executing.</span>
<Thread>.join() <spanclass="hljs-comment"># Waits for thread to finish.</span>
</code></pre></div>
<ul>
<li><strong>Use <codeclass="python hljs"><spanclass="hljs-string">'kwargs=<dict>'</span></code> to pass keyword arguments to the function.</strong></li>
<li><strong>Use <codeclass="python hljs"><spanclass="hljs-string">'daemon=True'</span></code>, or the program will not be able to exit while the thread is alive.</strong></li>
</ul>
<div><h3id="lock">Lock</h3><pre><codeclass="python language-python hljs"><lock> = RLock() <spanclass="hljs-comment"># Lock that can only be released by the owner.</span>
<lock>.acquire() <spanclass="hljs-comment"># Waits for lock to be available.</span>
<lock>.release() <spanclass="hljs-comment"># Makes lock available again.</span>
<div><h3id="lock">Lock</h3><pre><codeclass="python language-python hljs"><lock> = RLock() <spanclass="hljs-comment"># Lock that can only be released by the owner.</span>
<lock>.acquire() <spanclass="hljs-comment"># Waits for lock to be available.</span>
<lock>.release() <spanclass="hljs-comment"># Makes lock available again.</span>
<div><h3id="semaphoreeventbarrier">Semaphore, Event, Barrier</h3><pre><codeclass="python language-python hljs"><Semaphore> = Semaphore(value=<spanclass="hljs-number">1</span>) <spanclass="hljs-comment"># Lock that can be acquired by 'value' threads at once.</span>
<Event> = Event() <spanclass="hljs-comment"># Method wait() blocks until set() is called.</span>
<Barrier> = Barrier(n_times) <spanclass="hljs-comment"># Method wait() blocks until it's called 'n_times'.</span>
<div><h3id="semaphoreeventbarrier">Semaphore, Event, Barrier</h3><pre><codeclass="python language-python hljs"><Semaphore> = Semaphore(value=<spanclass="hljs-number">1</span>) <spanclass="hljs-comment"># Lock that can be acquired by 'value' threads.</span>
<Event> = Event() <spanclass="hljs-comment"># Method wait() blocks until set() is called.</span>
<Barrier> = Barrier(n_times) <spanclass="hljs-comment"># Method wait() blocks until it's called n_times.</span>
</code></pre></div>
<div><h3id="threadpoolexecutor">Thread Pool Executor</h3><pre><codeclass="python language-python hljs"><spanclass="hljs-keyword">from</span> concurrent.futures <spanclass="hljs-keyword">import</span> ThreadPoolExecutor
<spanclass="hljs-keyword">with</span> ThreadPoolExecutor(max_workers=<spanclass="hljs-keyword">None</span>) <spanclass="hljs-keyword">as</span> executor: <spanclass="hljs-comment"># Does not exit until done.</span>
<div><h3id="threadpoolexecutor">Thread Pool Executor</h3><p><strong>Object that manages thread execution.</strong></p><pre><codeclass="python language-python hljs"><spanclass="hljs-keyword">from</span> concurrent.futures <spanclass="hljs-keyword">import</span> ThreadPoolExecutor
</code></pre></div>
<div><h4id="future">Future:</h4><pre><codeclass="python language-python hljs"><bool> = <Future>.done() <spanclass="hljs-comment"># Checks if thread has finished executing.</span>
<obj> = <Future>.result() <spanclass="hljs-comment"># Waits for thread to finish and returns result.</span>
</code></pre></div>
<pre><codeclass="python language-python hljs"><Exec> = ThreadPoolExecutor([max_workers]) <spanclass="hljs-comment"># Use max_workers to limit the number of threads.</span>
<Exec>.shutdown(wait=<spanclass="hljs-keyword">True</span>) <spanclass="hljs-comment"># Or: `with ThreadPoolExecutor() as executor: …`</span>
</code></pre>
<pre><codeclass="python language-python hljs"><iter> = <Exec>.map(<func>, <args_1>, ...) <spanclass="hljs-comment"># A multithreaded and non-lazy map().</span>
<Futr> = <Exec>.submit(<func>, <arg_1>, ...) <spanclass="hljs-comment"># Starts a thread and returns its Future object.</span>
<bool> = <Futr>.done() <spanclass="hljs-comment"># Checks if thread has finished executing.</span>
<obj> = <Futr>.result() <spanclass="hljs-comment"># Waits for thread to finish and returns result.</span>
</code></pre>
<div><h3id="queue">Queue</h3><p><strong>A thread-safe FIFO queue. For LIFO queue use LifoQueue.</strong></p><pre><codeclass="python language-python hljs"><spanclass="hljs-keyword">from</span> queue <spanclass="hljs-keyword">import</span> Queue
<pre><codeclass="python language-python hljs"><Queue>.put(<el>) <spanclass="hljs-comment"># Blocks until queue stops being full.</span>
<Queue>.put_nowait(<el>) <spanclass="hljs-comment"># Raises queue.Full exception if full.</span>
<el> = <Queue>.get() <spanclass="hljs-comment"># Blocks until queue stops being empty.</span>
<el> = <Queue>.get_nowait() <spanclass="hljs-comment"># Raises queue.Empty exception if empty.</span>
<pre><codeclass="python language-python hljs"><Queue>.put(<el>) <spanclass="hljs-comment"># Blocks until queue stops being full.</span>
<Queue>.put_nowait(<el>) <spanclass="hljs-comment"># Raises queue.Full exception if full.</span>
<el> = <Queue>.get() <spanclass="hljs-comment"># Blocks until queue stops being empty.</span>
<el> = <Queue>.get_nowait() <spanclass="hljs-comment"># Raises queue.Empty exception if empty.</span>
</code></pre>
<div><h2id="operator"><ahref="#operator"name="operator">#</a>Operator</h2><p><strong>Module of functions that provide the functionality of operators.</strong></p><pre><codeclass="python language-python hljs"><spanclass="hljs-keyword">from</span> operator <spanclass="hljs-keyword">import</span> add, sub, mul, truediv, floordiv, mod, pow, neg, abs
<spanclass="hljs-keyword">from</span> operator <spanclass="hljs-keyword">import</span> eq, ne, lt, le, gt, ge
<memb> = <param>.kind <spanclass="hljs-comment"># Member of ParameterKind enum.</span>
</code></pre></div>
<div><h2id="metaprograming"><ahref="#metaprograming"name="metaprograming">#</a>Metaprograming</h2><p><strong>Code that generates code.</strong></p><div><h3id="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><codeclass="python language-python hljs"><class> = type(<spanclass="hljs-string">'<class_name>'</span>, <parents_tuple>, <attributes_dict>)</code></pre></div></div>