edri.abstract.worker package

Submodules

edri.abstract.worker.worker module

class edri.abstract.worker.worker.Worker(pipe: Connection, event: T | None, name: str, *args: Any, **kwargs: Any)[source]

Bases: Generic[T], ABC

A generic worker class designed to communicate with a manager through events.

Attributes:
  • _name (str) -- The name of the worker, used for logging purposes.

  • _manager_pipe (Connection) -- The communication pipe to the manager.

  • event (EventType) -- The initial event associated with the worker.

  • logger (Logger) -- Logger instance for logging messages.

Methods:
  • __init__ -- Initializes a new Worker instance.

  • do -- Abstract method defining the main logic of the worker; to be implemented by subclasses.

  • event_send -- Sends an event to the manager.

  • event_receive -- Receives an event from the manager, blocking if no event is available.

  • run -- Entry point for the worker's execution, wrapping the do method with error handling and cleanup.

property cancel_requested: bool
abstractmethod do() → None[source]

Abstract method that should be implemented by subclasses to define the worker's main logic.

event_receive() → Event[source]

Receives an event from the manager. This method blocks until an event is available.

Returns:

The received event.

Return type:

Event

event_send(event: Event) → None[source]

Sends an event to the manager.

Parameters:

event (Event) -- The event to be sent.

raise_if_cancelled() → None[source]
run() → None[source]

The main entry point for the worker's execution. It runs setup() then do(), handling any exceptions and ensuring cleanup and communication with the manager upon termination.

setup() → None[source]

Hook run once before do(), inside the same error-handling scope.

The default is a no-op. Override it for one-time preparation that must happen in the worker's own thread/process (building helpers, opening resources). A failure here is treated exactly like a failure in do(): it is logged and the worker still sends WorkerQuit so the manager can reap it.

wait_cancelled(timeout: float | None = None) → bool[source]
exception edri.abstract.worker.worker.WorkerCancelled[source]

Bases: Exception

Internal cooperative-cancellation signal raised inside a worker.

edri.abstract.worker.worker_process module

class edri.abstract.worker.worker_process.WorkerProcess(pipe: Connection, event: T, name: str, *args: Any, **kwargs: Any)[source]

Bases: Worker[T], Process, ABC

A WorkerProcess class that integrates functionality from a custom Worker class with multiprocessing capabilities, designed to run as a separate process. This class is abstract and should be subclassed to implement specific worker behavior in a multiprocessing environment.

Attributes:

Inherits all attributes from the Worker class and the Process class.

Methods:
  • __init__(pipe -- Connection, event, name: str): Initializes a new instance

  • of the WorkerProcess.

Parameters:
  • pipe (Connection) -- A connection object used for communication between

  • multiprocessing.Connection. (processes. This should be an instance of)

  • event -- The event type that the worker will handle. The specific type is

  • variable (determined by the EventType generic type)

  • be (which should)

  • subclass. (defined in the)

  • name (str) -- A name for the process. This is used internally by the

  • process (multiprocessing module to identify the)

  • for (especially useful)

  • debugging.

Returns:

The constructor initializes the process but does not return a value.

Return type:

None

Raises:
  • Inherits any exceptions raised by the Worker class or the Process class --

  • constructors. --

Note

As this class is abstract, it cannot be instantiated directly. It requires subclassing and implementation of the abstract methods defined in the Worker class.

edri.abstract.worker.worker_thread module

class edri.abstract.worker.worker_thread.WorkerThread(pipe: Connection, event: T, name: str, *args: Any, **kwargs: Any)[source]

Bases: Worker[T], Thread, ABC

A WorkerThread class that combines the custom Worker class with threading capabilities, designed to run as a separate thread. This class is abstract and should be subclassed to implement specific worker behavior in a threaded environment.

Note

As this class is abstract, it cannot be instantiated directly. It requires subclassing and implementation of the abstract methods defined in the Worker class.

The worker receives the same event instance and argument objects passed to start_worker(). EDRI does not copy them. Mutations made by the worker can therefore be visible to the caller; callers that require isolation must copy the input or construct a dedicated data-transfer event before starting the worker.

Module contents