upsies.utils.daemon

Background workers to keep the UI responsive

Functions

upsies.utils.daemon.maybe_terminate(input_queue)[source]

Read all queued items and raise DaemonProcessTerminated if there is a MsgType.terminate

Put all other items back into the queue in the same order.

upsies.utils.daemon.read_input_queue(input_queue, default=(None, None))[source]

Return single (<MsgType>, <payload data>) tuple from input_queue or default if it is empty

upsies.utils.daemon.read_input_queue_key(input_queue, key)[source]

Read dict from input_queue and return its value for key if it exists

The dict must be queued as MsgType.info.

If MsgType.terminate is queued, raise DaemonProcessTerminated instead of looking for key.

Put all other items back into the queue in the same order.

upsies.utils.daemon.read_input_queue_until_empty(input_queue)[source]

Keep reading input_queue until it is empty

Return the sequence of items that was read from the queue.

Raise DaemonProcessTerminated if there is a MsgType.terminate queued.

Classes

class upsies.utils.daemon.DaemonProcess(target, name=None, args=(), kwargs={}, init_callback=None, info_callback=None, error_callback=None, result_callback=None, finished_callback=None)[source]

Bases: object

multiprocessing.Process abstraction with IPC

Intended to offload heavy work (e.g. torrent creation) onto a different process. (Threads can still make the UI unresponsive because of the GIL.)

start()[source]

Start the process

stop()[source]

Stop the process

This has no effect if the process is not alive.

send(typ, value)[source]

Send message to process if it is still alive

If is_alive is False, do nothing.

Parameters:
  • typ (MsgType) – General message category

  • value – Message string, payload, sentinel, etc.

property is_alive

Whether start() was called and the process has not finished yet

property is_finished

Whether start() was called and the process has finished

property exception

Exception from target or callback, None if no exception was raised

async join()[source]

Block asynchronously until the process exits

class upsies.utils.daemon.MsgType(value)[source]

Bases: Enum

Enum that specifies the type of an IPC message (info, error, etc)