From d5c9a5cc1b44ba0b448a7687735f40d08519244e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= <sornjure@gmail.com> Date: Fri, 2 Aug 2019 23:55:01 +0200 Subject: [PATCH] Threading --- README.md | 29 ++++++++++++++++------------- index.html | 27 +++++++++++++++------------ 2 files changed, 31 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 0621d55..e0c12c1 100644 --- a/README.md +++ b/README.md @@ -1948,17 +1948,6 @@ from collections import deque <deque>.rotate(n=1) # Rotates elements to the right. ``` -### Example -```python ->>> a = deque([1, 2, 3], maxlen=3) ->>> a.append(4) -[2, 3, 4] ->>> a.appendleft(5) -[5, 2, 3] ->>> a.insert(1, 6) -IndexError: deque already at its maximum size -``` - Threading --------- @@ -2001,8 +1990,22 @@ with ThreadPoolExecutor(max_workers=None) as executor: ``` ```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 +* **A thread-safe FIFO queue. For LIFO queue use 'queue.LifoQueue'.** +```python +from queue import Queue +<Queue> = Queue(maxsize=0) +``` + +```python +<Queue>.put(<el>) # Blocks until queue has a free spot. +<Queue>.put_nowait(<el>) # Raises queue.Full exception if full. +<el> = <Queue>.get() # Blocks until queue contains an item. +<el> = <Queue>.get_nowait() # Raises _queue.Empty exception if empty. ``` diff --git a/index.html b/index.html index f7274f0..95a2edd 100644 --- a/index.html +++ b/index.html @@ -1720,15 +1720,6 @@ cursor.execute(<span class="hljs-string">'<query>'</span>, <dict/namedt <deque>.extendleft(<collection>) <span class="hljs-comment"># Collection gets reversed.</span> <deque>.rotate(n=<span class="hljs-number">1</span>) <span class="hljs-comment"># Rotates elements to the right.</span> </code></pre> -<div><h3 id="example-2">Example</h3><pre><code class="python language-python hljs"><span class="hljs-meta">>>> </span>a = deque([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>], maxlen=<span class="hljs-number">3</span>) -<span class="hljs-meta">>>> </span>a.append(<span class="hljs-number">4</span>) -[<span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>] -<span class="hljs-meta">>>> </span>a.appendleft(<span class="hljs-number">5</span>) -[<span class="hljs-number">5</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>] -<span class="hljs-meta">>>> </span>a.insert(<span class="hljs-number">1</span>, <span class="hljs-number">6</span>) -IndexError: deque already at its maximum size -</code></pre></div> - <div><h2 id="threading"><a href="#threading" name="threading">#</a>Threading</h2><ul> <li><strong>CPython interpreter can only run a single thread at the time.</strong></li> <li><strong>That is why using multiple threads won't result in a faster execution, unless there is an I/O operation in the thread.</strong></li> @@ -1760,8 +1751,20 @@ lock.release() <Future> = executor.submit(<function>, <arg_1>, ...) </code></pre></div> -<pre><code class="python language-python hljs"><bool> = <Future>.done() <span class="hljs-comment"># Checks if thread has finished executing.</span> -<obj> = <Future>.result() <span class="hljs-comment"># Waits for thread to finish and returns result.</span> +<pre><code class="python language-python hljs"><bool> = <Future>.done() <span class="hljs-comment"># Checks if thread has finished executing.</span> +<obj> = <Future>.result() <span class="hljs-comment"># Waits for thread to finish and returns result.</span> +</code></pre> +<div><h3 id="queue">Queue</h3><ul> +<li><strong>A thread-safe FIFO queue. For LIFO queue use 'queue.LifoQueue'.</strong></li> +</ul><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> queue <span class="hljs-keyword">import</span> Queue +<Queue> = Queue(maxsize=<span class="hljs-number">0</span>) +</code></pre></div> + + +<pre><code class="python language-python hljs"><Queue>.put(<el>) <span class="hljs-comment"># Blocks until queue has a free spot.</span> +<Queue>.put_nowait(<el>) <span class="hljs-comment"># Raises queue.Full exception if full.</span> +<el> = <Queue>.get() <span class="hljs-comment"># Blocks until queue contains an item.</span> +<el> = <Queue>.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><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 @@ -2144,7 +2147,7 @@ right = [[<span class="hljs-number">0.1</span> , <span class="hljs-number">0.6< right = [[<span class="hljs-number">0.1</span>, <span class="hljs-number">0.6</span>, <span class="hljs-number">0.8</span>], [<span class="hljs-number">0.1</span>, <span class="hljs-number">0.6</span>, <span class="hljs-number">0.8</span>], [<span class="hljs-number">0.1</span>, <span class="hljs-number">0.6</span>, <span class="hljs-number">0.8</span>]] <span class="hljs-comment"># Shape: (3, 3) <- !</span> </code></pre></div> -<div><h4 id="3ifneithernonmatchingdimensionhassize1riseanerror">3. If neither non-matching dimension has size 1, rise an error.</h4><div><h3 id="example-3">Example</h3><div><h4 id="foreachpointreturnsindexofitsnearestpoint010608121">For each point returns index of its nearest point (<code class="python hljs">[<span class="hljs-number">0.1</span>, <span class="hljs-number">0.6</span>, <span class="hljs-number">0.8</span>] => [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">1</span>]</code>):</h4><pre><code class="python language-python hljs"><span class="hljs-meta">>>> </span>points = np.array([<span class="hljs-number">0.1</span>, <span class="hljs-number">0.6</span>, <span class="hljs-number">0.8</span>]) +<div><h4 id="3ifneithernonmatchingdimensionhassize1riseanerror">3. If neither non-matching dimension has size 1, rise an error.</h4><div><h3 id="example-2">Example</h3><div><h4 id="foreachpointreturnsindexofitsnearestpoint010608121">For each point returns index of its nearest point (<code class="python hljs">[<span class="hljs-number">0.1</span>, <span class="hljs-number">0.6</span>, <span class="hljs-number">0.8</span>] => [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">1</span>]</code>):</h4><pre><code class="python language-python hljs"><span class="hljs-meta">>>> </span>points = np.array([<span class="hljs-number">0.1</span>, <span class="hljs-number">0.6</span>, <span class="hljs-number">0.8</span>]) [ <span class="hljs-number">0.1</span>, <span class="hljs-number">0.6</span>, <span class="hljs-number">0.8</span>] <span class="hljs-meta">>>> </span>wrapped_points = points.reshape(<span class="hljs-number">3</span>, <span class="hljs-number">1</span>) [[ <span class="hljs-number">0.1</span>],