diff --git a/README.md b/README.md index 5f95d09..c9dc177 100644 --- a/README.md +++ b/README.md @@ -2050,19 +2050,19 @@ from threading import Thread, RLock, Semaphore, Event, Barrier ### Thread ```python - = Thread(target=) # Use `args=` to set arguments. -.start() # Starts the thread. - = .is_alive() # Checks if thread has finished executing. -.join() # Waits for thread to finish. + = Thread(target=) # Use `args=` to set arguments. +.start() # Starts the thread. + = .is_alive() # Checks if thread has finished executing. +.join() # Waits for thread to finish. ``` * **Use `'kwargs='` to pass keyword arguments to the function.** * **Use `'daemon=True'`, or the program will not be able to exit while the thread is alive.** ### Lock ```python - = RLock() # Lock that can only be released by the owner. -.acquire() # Waits for lock to be available. -.release() # Makes lock available again. + = RLock() # Lock that can only be released by the owner. +.acquire() # Waits for lock to be available. +.release() # Makes lock available again. ``` #### Or: @@ -2074,24 +2074,27 @@ with lock: ### Semaphore, Event, Barrier ```python - = Semaphore(value=1) # Lock that can be acquired by 'value' threads at once. - = Event() # Method wait() blocks until set() is called. - = Barrier(n_times) # Method wait() blocks until it's called 'n_times'. + = Semaphore(value=1) # Lock that can be acquired by 'value' threads. + = Event() # Method wait() blocks until set() is called. + = Barrier(n_times) # Method wait() blocks until it's called n_times. ``` ### Thread Pool Executor +**Object that manages thread execution.** ```python from concurrent.futures import ThreadPoolExecutor -with ThreadPoolExecutor(max_workers=None) as executor: # Does not exit until done. - = 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( [, , ...]) # Also visible outside block. ``` -#### Future: ```python - = .done() # Checks if thread has finished executing. - = .result() # Waits for thread to finish and returns result. + = ThreadPoolExecutor([max_workers]) # Use max_workers to limit the number of threads. +.shutdown(wait=True) # Or: `with ThreadPoolExecutor() as executor: …` +``` + +```python + = .map(, , ...) # A multithreaded and non-lazy map(). + = .submit(, , ...) # Starts a thread and returns its Future object. + = .done() # Checks if thread has finished executing. + = .result() # Waits for thread to finish and returns result. ``` ### Queue @@ -2102,10 +2105,10 @@ from queue import Queue ``` ```python -.put() # Blocks until queue stops being full. -.put_nowait() # Raises queue.Full exception if full. - = .get() # Blocks until queue stops being empty. - = .get_nowait() # Raises queue.Empty exception if empty. +.put() # Blocks until queue stops being full. +.put_nowait() # Raises queue.Full exception if full. + = .get() # Blocks until queue stops being empty. + = .get_nowait() # Raises queue.Empty exception if empty. ``` @@ -2155,10 +2158,10 @@ delattr(, '') # Equivalent to `del . = signature() -no_of_params = len(.parameters) -param_names = list(.parameters.keys()) -param_kinds = [a.kind for a in .parameters.values()] + = signature() # Signature object of the function. + = .parameters # Dict of function's parameters. + = .name # Prameter's name. + = .kind # Member of ParameterKind enum. ``` diff --git a/index.html b/index.html index 5075936..f94de79 100644 --- a/index.html +++ b/index.html @@ -1835,19 +1835,19 @@ CompletedProcess(args=['bc', Thread
<Thread> = Thread(target=<function>)  # Use `args=<collection>` to set arguments.
-<Thread>.start()                      # Starts the thread.
-<bool> = <Thread>.is_alive()          # Checks if thread has finished executing.
-<Thread>.join()                       # Waits for thread to finish.
+

Thread

<Thread> = Thread(target=<function>)          # Use `args=<collection>` to set arguments.
+<Thread>.start()                              # Starts the thread.
+<bool> = <Thread>.is_alive()                  # Checks if thread has finished executing.
+<Thread>.join()                               # Waits for thread to finish.
 
  • Use 'kwargs=<dict>' to pass keyword arguments to the function.
  • Use 'daemon=True', or the program will not be able to exit while the thread is alive.
-

Lock

<lock> = RLock()                      # Lock that can only be released by the owner.
-<lock>.acquire()                      # Waits for lock to be available.
-<lock>.release()                      # Makes lock available again.
+

Lock

<lock> = RLock()                              # Lock that can only be released by the owner.
+<lock>.acquire()                              # Waits for lock to be available.
+<lock>.release()                              # Makes lock available again.
 

Or:

lock = RLock()
@@ -1855,31 +1855,32 @@ CompletedProcess(args=['bc', Semaphore, Event, Barrier
<Semaphore> = Semaphore(value=1)      # Lock that can be acquired by 'value' threads at once.
-<Event>     = Event()                 # Method wait() blocks until set() is called.
-<Barrier>   = Barrier(n_times)        # Method wait() blocks until it's called 'n_times'.
+

Semaphore, Event, Barrier

<Semaphore> = Semaphore(value=1)              # Lock that can be acquired by 'value' threads.
+<Event>     = Event()                         # Method wait() blocks until set() is called.
+<Barrier>   = Barrier(n_times)                # Method wait() blocks until it's called n_times.
 
-

Thread Pool Executor

from concurrent.futures import ThreadPoolExecutor
-with ThreadPoolExecutor(max_workers=None) as executor:         # Does not exit until done.
-    <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>, ...])    # Also visible outside block.
+

Thread Pool Executor

Object that manages thread execution.

from concurrent.futures import ThreadPoolExecutor
 
-

Future:

<bool> = <Future>.done()              # Checks if thread has finished executing.
-<obj>  = <Future>.result()            # Waits for thread to finish and returns result.
-
+
<Exec> = ThreadPoolExecutor([max_workers])    # Use max_workers to limit the number of threads.
+<Exec>.shutdown(wait=True)                    # Or: `with ThreadPoolExecutor() as executor: …`
+
+
<iter> = <Exec>.map(<func>, <args_1>, ...)    # A multithreaded and non-lazy map().
+<Futr> = <Exec>.submit(<func>, <arg_1>, ...)  # Starts a thread and returns its Future object.
+<bool> = <Futr>.done()                        # Checks if thread has finished executing.
+<obj>  = <Futr>.result()                      # Waits for thread to finish and returns result.
+

Queue

A thread-safe FIFO queue. For LIFO queue use LifoQueue.

from queue import Queue
 <Queue> = Queue(maxsize=0)
 
-
<Queue>.put(<el>)                     # Blocks until queue stops being full.
-<Queue>.put_nowait(<el>)              # Raises queue.Full exception if full.
-<el> = <Queue>.get()                  # Blocks until queue stops being empty.
-<el> = <Queue>.get_nowait()           # Raises queue.Empty exception if empty.
+
<Queue>.put(<el>)                             # Blocks until queue stops being full.
+<Queue>.put_nowait(<el>)                      # Raises queue.Full exception if full.
+<el> = <Queue>.get()                          # Blocks until queue stops being empty.
+<el> = <Queue>.get_nowait()                   # Raises queue.Empty exception if empty.
 

#Operator

Module of functions that provide the functionality of operators.

from operator import add, sub, mul, truediv, floordiv, mod, pow, neg, abs
 from operator import eq, ne, lt, le, gt, ge
@@ -1913,10 +1914,10 @@ delattr(<object>, '<attr_name>')
 

Parameters

from inspect import signature
-<sig>        = signature(<function>)
-no_of_params = len(<sig>.parameters)
-param_names  = list(<sig>.parameters.keys())
-param_kinds  = [a.kind for a in <sig>.parameters.values()]
+<sig>  = signature(<function>)             # Signature object of the function.
+<dict> = <sig>.parameters                  # Dict of function's parameters.
+<str>  = <param>.name                      # Prameter's name.
+<memb> = <param>.kind                      # Member of ParameterKind enum.
 

#Metaprograming

Code that generates code.

Type

Type is the root class. If only passed an object it returns its type (class). Otherwise it creates a new class.

<class> = type('<class_name>', <parents_tuple>, <attributes_dict>)