diff --git a/README.md b/README.md index 3ed3892..26e0643 100644 --- a/README.md +++ b/README.md @@ -1962,6 +1962,8 @@ IndexError: deque already at its maximum size Threading --------- +* **CPython interpreter can only run a single thread at the time.** +* **That is why using multiple threads won't result in a faster execution, unless there is an I/O operation in the thread.** ```python from threading import Thread, RLock ``` @@ -1993,10 +1995,15 @@ with lock: ```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) - results = executor.map(lambda x, y: x + y, 'abc', '123') # ('a1', 'b2', 'c3') + = executor.map(lambda x: x + 1, range(3)) # (1, 2, 3) + = executor.map(lambda x, y: x + y, 'abc', '123') # ('a1', 'b2', 'c3') + = executor.submit(, , ...) +``` + +```python + = .done() # Checks if thread has finished executing. + = .result() # Waits for thread to finish and returns result. ``` -* **CPython interpreter can only run a single thread at the time. That is why this map() won't be faster than the standard map(), unless passed function contains an I/O operation.** Operator diff --git a/index.html b/index.html index 49d62e0..5e42d13 100644 --- a/index.html +++ b/index.html @@ -1729,9 +1729,13 @@ cursor.execute('<query>', <dict/namedt IndexError: deque already at its maximum size -

#Threading

from threading import Thread, RLock
+

#Threading

    +
  • CPython interpreter can only run a single thread at the time.
  • +
  • That is why using multiple threads won't result in a faster execution, unless there is an I/O operation in the thread.
  • +
from threading import Thread, RLock
 
+

Thread

thread = Thread(target=<function>, args=(<first_arg>, ))
 thread.start()
 ...
@@ -1751,13 +1755,14 @@ lock.release()
 
 

Thread Pool

from concurrent.futures import ThreadPoolExecutor
 with ThreadPoolExecutor(max_workers=None) as executor:
-    results = executor.map(lambda x: x + 1, range(3))         # (1, 2, 3)
-    results = executor.map(lambda x, y: x + y, 'abc', '123')  # ('a1', 'b2', 'c3')
+    <iter>   = executor.map(lambda x: x + 1, range(3))         # (1, 2, 3)
+    <iter>   = executor.map(lambda x, y: x + y, 'abc', '123')  # ('a1', 'b2', 'c3')
+    <Future> = executor.submit(<function>, <arg_1>, ...)
 
-
    -
  • CPython interpreter can only run a single thread at the time. That is why this map() won't be faster than the standard map(), unless passed function contains an I/O operation.
  • -
+
<bool> = <Future>.done()    # Checks if thread has finished executing.
+<obj>  = <Future>.result()  # Waits for thread to finish and returns result.
+

#Operator

from operator import add, sub, mul, truediv, floordiv, mod, pow, neg, abs
 from operator import eq, ne, lt, le, gt, ge
 from operator import and_, or_, not_