Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 08b4c8a

Browse files
committed
Threading
1 parent f9f9968 commit 08b4c8a

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1170,7 +1170,8 @@ Hello World!
11701170
with open('<path>') as file: ...
11711171
with wave.open('<path>') as wave_file: ...
11721172
with memoryview(<bytes/bytearray/array>) as view: ...
1173-
db = sqlite3.connect('<path>'); with db: db.execute('<insert_query>')
1173+
with concurrent.futures.ThreadPoolExecutor() as executor: ...
1174+
db = sqlite3.connect('<path>'); with db: ...
11741175
lock = threading.RLock(); with lock: ...
11751176
```
11761177

@@ -1982,6 +1983,13 @@ with lock:
19821983
...
19831984
```
19841985

1986+
### Thread Pool
1987+
```python
1988+
from concurrent.futures import ThreadPoolExecutor
1989+
with ThreadPoolExecutor(max_workers=None) as executor:
1990+
results = executor.map(lambda x: x + 1, range(3)) # (1, 2, 3)
1991+
```
1992+
19851993

19861994
Introspection
19871995
-------------

index.html

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1101,7 +1101,8 @@
11011101
<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: ...
11021102
<span class="hljs-keyword">with</span> wave.open(<span class="hljs-string">'&lt;path&gt;'</span>) <span class="hljs-keyword">as</span> wave_file: ...
11031103
<span class="hljs-keyword">with</span> memoryview(&lt;bytes/bytearray/array&gt;) <span class="hljs-keyword">as</span> view: ...
1104-
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>)
1104+
<span class="hljs-keyword">with</span> concurrent.futures.ThreadPoolExecutor() <span class="hljs-keyword">as</span> executor: ...
1105+
db = sqlite3.connect(<span class="hljs-string">'&lt;path&gt;'</span>); <span class="hljs-keyword">with</span> db: ...
11051106
lock = threading.RLock(); <span class="hljs-keyword">with</span> lock: ...
11061107
</code></pre></div>
11071108

@@ -1740,6 +1741,11 @@
17401741
...
17411742
</code></pre></div>
17421743

1744+
<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
1745+
<span class="hljs-keyword">with</span> ThreadPoolExecutor(max_workers=<span class="hljs-keyword">None</span>) <span class="hljs-keyword">as</span> executor:
1746+
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>
1747+
</code></pre></div>
1748+
17431749
<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>
17441750
&lt;dict&gt; = locals() <span class="hljs-comment"># Dict of local variables. Also vars().</span>
17451751
&lt;dict&gt; = globals() <span class="hljs-comment"># Dict of global variables.</span>

0 commit comments

Comments
 (0)