Threading
to run the function in a new thread (non-daemon)
import threading
thread_variable = threading.Thread(target=function_name)
thread_variable.start()
to call the function back to the main thread
thread_variable.join()
to run the function in a daemon thread
When the main thread exits, the daemon thread(s) will automatically exits.
thread_variable.daemon = True
thread_variable.start()
to set a mutual exclusion
lock_variable = threading.Lock()
to protect "my" access
Will wait if someone else is accessing
lock_variable.acquire()
to protect "my" access without waiting
Will give up immediately if not available
lock_variable.acquire(blocking=False)
to release "my" access
lock_variable.release()
to use Reentrant lock
This allows to lock multiple times in the same thread. It has to unlock as many as locking. Useful in a recurrsion.
rlock_variable = threading.RLock()
rlock_variable.acquire()
rlock_variable.release()
to lock conditionally
lock_condition_variable = threading.Condition(lock=a_variable)
to wait until being notified by other threads
lock_condition_variable.wait()
to notify another thread
Should call before exiting a thread
lock_condition_variable.notify()
to notify all other threads before exiting a thread
Should call before exiting a thread
lock_condition_variable.notify_all()
to use Read/Write lock
from readerwriterlock import rwlock
rwlock_variable = rwlock.RWLockFair()
to lock for reading
read_lock_variable = rwlock_variable.gen_rlock()
read_lock_variable.acquire()
read_lock_variable.release()
to lock for writing
write_lock_variable = rwlock_variable.gen_wlock()
write_lock_variable.acquire()
write_lock_variable.release()
to use Semaphore
Semaphore manages a counter representing the number of release() calls minus the number of acquire() calls.
lock_variable = threading.Semaphore(1)
Thread Pool
Maintain a collection of threads and re-use them.
from concurrent.futures import ThreadPoolExecutor
pool = ThreadPoolExecutor(max_workers=5)
pool.submit(function_name)
pool.shutdown()
Future
from concurrent.futures import ThreadPoolExecutor
pool = ThreadPoolExecutor(max_workers=5)
future = pool.submit(function_name)
future.result()
Multiprocessing
to start the function in a new process
import multiprocessing
process_variable = multiprocessing.Process(target=function_name)
process_variable.start()
Process Pool
Main a collection of threads and re-use them.
from concurrent.futures import ProcessPoolExecutor
pool = ProcessPoolExecutor(max_workers=5)
pool.submit(function_name)
pool.shutdown()