embutils.utils.threading module
Threading utilities.
- date
2021
- author
Christian Wiche
- contact
- license
The MIT License (MIT)
- class embutils.utils.threading.AbstractThreadTask[source]
Bases:
abc.ABCThread task abstraction. Use this class to define how to execute a task inside the ThreadPool.
- _abc_impl = <_abc_data object>
- embutils.utils.threading.SDK_TP = <embutils.utils.threading.ThreadPool object>
Embutils internal thread pool
- class embutils.utils.threading.SimpleThreadTask(task: Callable[[...], None], *args, name: str = 'Unnamed', **kwargs)[source]
Bases:
embutils.utils.threading.AbstractThreadTaskSimple thread task. Accepts a function to be executed by a worker on the ThreadPool.
Class initialization.
- Parameters
task (Callable[..., None]) – Task functionality.
name (str) – Task name.
args – Task arguments.
kwargs – Task keyword arguments.
- _abc_impl = <_abc_data object>
- class embutils.utils.threading.ThreadPool(size: int, name: str, timeout: float = 0.1, daemon: bool = True)[source]
Bases:
objectSimple thread pool implementation. Use queues to coordinate tasks among a set of worker threads.
Class initialization.
- Parameters
size (int) – Size of the thread pool.
name (str) – Thread pool name. Used as prefix on the workers as <name>_<worker_id>.
timeout (float) – Polling time used by the workers while waiting on a get() request on the task queue. This value mainly affects the time used by the threads to terminate when terminate() is called.
daemon (bool) – Set to true if the threads should immediately terminate when the main thread exists.
- Raises
ValueError – Minimum workers count is not met. Polling timeout needs to be a positive number.
- property active: int
Number of active workers.
- enqueue(task: embutils.utils.threading.AbstractThreadTask) None[source]
Enqueue a task on the thread pool to be executed by the workers.
- Parameters
task (AbstractThreadTask) – Task to added to the queue.
- property size: int
Number of workers.
- class embutils.utils.threading.ThreadWorker(name: str, tasks: queue.Queue, timeout: float)[source]
Bases:
threading.ThreadThread pool worker. This represents a single thread on the pool. The thread is set as daemon or not based on the pool configurations.
Class initialization.
- Parameters
name (str) – Worker name.
tasks (Queue) – Queue to get the tasks from.
timeout (float) – Timeout for waiting for a task.
- embutils.utils.threading.get_threads(name: Optional[str] = None, alive: bool = False) List[threading.Thread][source]
Return all the live threads.
- Parameters
name (str) – Filter. If provided, return threads that have or contain this name.
alive (bool) – Filter. If enabled, return alive threads only.
- Returns
List with named threads.
- Return type
list
- embutils.utils.threading.sync(lock_name: str) Callable[[Callable[[...], Any]], Callable[[...], Any]][source]
Decorator. Used to wrap a class method with a given lock attribute.
- Parameters
lock_name (str) – Lock attribute name.
- Returns
Decorated function wrapped on the given lock.
- Return type
Callable[[Callable[…, RT]], Callable[…, RT]]