From 888c8a29c1d84ec8648a94815aafb99eaf1d296e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= Date: Sun, 24 May 2020 17:16:18 +0200 Subject: [PATCH] Threading --- README.md | 34 ++++++++++++++++------------------ index.html | 34 ++++++++++++++++------------------ 2 files changed, 32 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index 4ceb461..f4d14f8 100644 --- a/README.md +++ b/README.md @@ -2047,21 +2047,19 @@ from threading import Thread, RLock, Semaphore, Event, Barrier ### Thread ```python -thread = Thread(target=, args=(, )) -thread.start() -... - = thread.is_alive() # Checks if thread has finished executing. -thread.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 -lock = RLock() -lock.acquire() # Waits for lock to be available. -... -lock.release() + = RLock() +.acquire() # Waits for lock to be available. +.release() # Makes the lock available again. ``` #### Or: @@ -2073,9 +2071,9 @@ with lock: ### Semaphore, Event, Barrier ```python - = Semaphore(value=1) # Lock that can be acquired 'value' times. - = 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 'value' times. + = Event() # Method wait() blocks until set() is called. + = Barrier(n_times) # Method wait() blocks until it's called 'n_times'. ``` ### Thread Pool Executor @@ -2089,8 +2087,8 @@ with ThreadPoolExecutor(max_workers=None) as executor: # Does not exit u #### Future: ```python - = .done() # Checks if thread has finished executing. - = .result() # Waits for thread to finish and returns result. + = .done() # Checks if thread has finished executing. + = .result() # Waits for thread to finish and returns result. ``` ### Queue @@ -2101,10 +2099,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. ``` diff --git a/index.html b/index.html index 736949a..3832124 100644 --- a/index.html +++ b/index.html @@ -1805,21 +1805,19 @@ CompletedProcess(args=['bc', Thread
thread = Thread(target=<function>, args=(<first_arg>, ))
-thread.start()
-...
-<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.acquire()                       # Waits for lock to be available.
-...
-lock.release()
+

Lock

<lock> = RLock()
+<lock>.acquire()                      # Waits for lock to be available.
+<lock>.release()                      # Makes the lock available again.
 

Or:

lock = RLock()
@@ -1827,9 +1825,9 @@ lock.release()
     ...
 
-

Semaphore, Event, Barrier

<Semaphore> = Semaphore(value=1)     # Lock that can be acquired 'value' times.
-<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 'value' times.
+<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
@@ -1839,8 +1837,8 @@ lock.release()
     <Future> = executor.submit(<function> [, <arg_1>, ...])    # Also visible outside block.
 
-

Future:

<bool> = <Future>.done()             # Checks if thread has finished executing.
-<obj>  = <Future>.result()           # Waits for thread to finish and returns result.
+

Future:

<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 LifoQueue.

from queue import Queue
@@ -1848,10 +1846,10 @@ lock.release()
 
-
<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