Browse Source

Threading

pull/41/head
Jure Šorn 5 years ago
parent
commit
08b4c8a9ce
2 changed files with 16 additions and 2 deletions
  1. 10
      README.md
  2. 8
      index.html

10
README.md

@ -1170,7 +1170,8 @@ Hello World!
with open('<path>') as file: ...
with wave.open('<path>') as wave_file: ...
with memoryview(<bytes/bytearray/array>) as view: ...
db = sqlite3.connect('<path>'); with db: db.execute('<insert_query>')
with concurrent.futures.ThreadPoolExecutor() as executor: ...
db = sqlite3.connect('<path>'); with db: ...
lock = threading.RLock(); with lock: ...
```
@ -1982,6 +1983,13 @@ with lock:
...
```
### Thread Pool
```python
from concurrent.futures import ThreadPoolExecutor
with ThreadPoolExecutor(max_workers=None) as executor:
results = executor.map(lambda x: x + 1, range(3)) # (1, 2, 3)
```
Introspection
-------------

8
index.html

@ -1101,7 +1101,8 @@ Hello World!
<div><h4 id="listofexistingcontextmanagers">List of existing context managers:</h4><pre><code class="python language-python hljs"><span class="hljs-keyword">with</span> open(<span class="hljs-string">'&lt;path&gt;'</span>) <span class="hljs-keyword">as</span> file: ...
<span class="hljs-keyword">with</span> wave.open(<span class="hljs-string">'&lt;path&gt;'</span>) <span class="hljs-keyword">as</span> wave_file: ...
<span class="hljs-keyword">with</span> memoryview(&lt;bytes/bytearray/array&gt;) <span class="hljs-keyword">as</span> view: ...
db = sqlite3.connect(<span class="hljs-string">'&lt;path&gt;'</span>); <span class="hljs-keyword">with</span> db: db.execute(<span class="hljs-string">'&lt;insert_query&gt;'</span>)
<span class="hljs-keyword">with</span> concurrent.futures.ThreadPoolExecutor() <span class="hljs-keyword">as</span> executor: ...
db = sqlite3.connect(<span class="hljs-string">'&lt;path&gt;'</span>); <span class="hljs-keyword">with</span> db: ...
lock = threading.RLock(); <span class="hljs-keyword">with</span> lock: ...
</code></pre></div>
@ -1740,6 +1741,11 @@ lock.release()
...
</code></pre></div>
<div><h3 id="threadpool">Thread Pool</h3><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> concurrent.futures <span class="hljs-keyword">import</span> ThreadPoolExecutor
<span class="hljs-keyword">with</span> ThreadPoolExecutor(max_workers=<span class="hljs-keyword">None</span>) <span class="hljs-keyword">as</span> executor:
results = 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>
</code></pre></div>
<div><h2 id="introspection"><a href="#introspection" name="introspection">#</a>Introspection</h2><p><strong>Inspecting code at runtime.</strong></p><div><h3 id="variables">Variables</h3><pre><code class="python language-python hljs">&lt;list&gt; = dir() <span class="hljs-comment"># Names of variables in current scope.</span>
&lt;dict&gt; = locals() <span class="hljs-comment"># Dict of local variables. Also vars().</span>
&lt;dict&gt; = globals() <span class="hljs-comment"># Dict of global variables.</span>

Loading…
Cancel
Save