mode.threads — mode-ng 0.4.0 documentation

# `mode.threads`

ServiceThread - Service that starts in a separate thread.

Will use the default thread pool executor (`loop.set_default_executor()`), unless you specify a specific executor instance.

Note: To stop something using the thread’s loop, you have to use the `on_thread_stop` callback instead of the on_stop callback.

class mode.threads.QueueServiceThread(*, loop: Optional[AbstractEventLoop] = None, thread_loop: Optional[AbstractEventLoop] = None, Worker: Optional[Type[WorkerThread]] = None, **kwargs: Any)

  Service running in separate thread.

  Uses a queue to run functions inside the thread, so you can delegate calls.

  abstract: ClassVar[bool] = False

    Set to True if this service class is abstract-only, meaning it will only be used as a base class.

  async call_thread(fun: Callable[[...], Awaitable], *args: Any, **kwargs: Any) → Any
  async cast_thread(fun: Callable[[...], Awaitable], *args: Any, **kwargs: Any) → None
  logger: logging.Logger = <Logger mode.threads (WARNING)>
  property method_queue: MethodQueue
  async on_thread_started() → None
  async on_thread_stop() → None

class mode.threads.QueuedMethod(promise: asyncio.Future, method: Callable[..., Awaitable[Any]], args: tuple[Any, ...], kwargs: dict[str, Any])

  Describe a method to be called by thread.

  args: tuple[Any, ...]

    Alias for field number 2

  kwargs: dict[str, Any]

    Alias for field number 3

  method: Callable[[...], Awaitable[Any]]

    Alias for field number 1

  promise: Future

    Alias for field number 0

class mode.threads.ServiceThread(*, loop: Optional[AbstractEventLoop] = None, thread_loop: Optional[AbstractEventLoop] = None, Worker: Optional[Type[WorkerThread]] = None, **kwargs: Any)

  Service subclass running within a dedicated thread.

  Worker

    alias of `WorkerThread`

  abstract: ClassVar[bool] = False

    Set to True if this service class is abstract-only, meaning it will only be used as a base class.

  async crash(exc: BaseException) → None

    Crash the service and all child services.

  last_wakeup_at: float = 0.0
  logger: logging.Logger = <Logger mode.threads (WARNING)>
  async maybe_start() → bool

    Start the service, if it has not already been started.

  on_crash(msg: str, *fmt: Any, **kwargs: Any) → None
  async on_thread_started() → None
  async on_thread_stop() → None
  set_shutdown() → None

    Set the shutdown signal.

    Notes

    If `wait_for_shutdown` is set, stopping the service will wait for this flag to be set.

  async start() → None
  async stop() → None

    Stop the service.

  wait_for_shutdown: bool = True

    Set to True if .stop must wait for the shutdown flag to be set.

  wait_for_thread: bool = True

    Set this to False if s.start() should not wait for the underlying thread to be fully started.

class mode.threads.WorkerThread(service: ServiceThread, **kwargs: Any)

  Thread class used for services running in a dedicated thread.

  is_stopped: Event
  run() → None

    Method representing the thread’s activity.

    You may override this method in a subclass. The standard run() method invokes the callable object passed to the object’s constructor as the target argument, if any, with sequential and keyword arguments taken from the args and kwargs arguments, respectively.

  service: ServiceThread
  stop() → None