embutils.utils.threading module

Threading utilities.

date

2021

author

Christian Wiche

contact

cwichel@gmail.com

license

The MIT License (MIT)

class embutils.utils.threading.AbstractThreadTask[source]

Bases: abc.ABC

Thread task abstraction. Use this class to define how to execute a task inside the ThreadPool.

_abc_impl = <_abc_data object>
abstract execute() None[source]

Execution called by the ThreadWorkers on the ThreadPool implementation.

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

Simple 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>
execute() None[source]

Execution called by the ThreadWorkers on the ThreadPool implementation.

class embutils.utils.threading.ThreadPool(size: int, name: str, timeout: float = 0.1, daemon: bool = True)[source]

Bases: object

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

_create_workers() None[source]

Create all the pool workers.

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.

stop() None[source]

Wait for the task queue to be completed and stop all the workers.

class embutils.utils.threading.ThreadWorker(name: str, tasks: queue.Queue, timeout: float)[source]

Bases: threading.Thread

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

run() None[source]

Runs the worker logic.

  • Blocks until retrieves a task from the tasks queue (locked).

  • Checks that the task is a callable and execute.

stop() None[source]

Stops the thread.

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]]