pyppin.threading.periodic_task

[View Source]

Run a function periodically in a background thread.

Classes

PeriodicTask(function, period[, name, ...])

Execute function() at regular intervals in a background thread.

class pyppin.threading.periodic_task.PeriodicTask(function: Callable[[], None], period: Union[timedelta, float], name: Optional[str] = None, wait_for_first: bool = False, die_on_exception: bool = False, death_signal: int = Signals.SIGTERM, _test_clock: Optional[Callable[[], float]] = None)[source]

Bases: Thread

Execute function() at regular intervals in a background thread.

Parameters
  • function – The function to be called.

  • period – The interval between successive calls to the function, either as a timedelta or in seconds. Note that this is measured from the start of one call to the start of the next.

  • name – The name for the thread, defaulting to the function name.

  • wait_for_first – If true, the constructor will block until the first call to the function has finished.

  • die_on_exception – If function raises an uncaught exception, we will always print the stack trace to stderr – but what should we do next? Python doesn’t propagate exceptions in child threads to the parent thread. If this flag is False, we do nothing, and simply call the function again next time. If it is True, we will instead kill the parent process on exception, providing a sort of simulacrum of what would have happened had this failed in the main thread.

  • death_signal – The signal we will use to kill this process if die_on_exception is True.

This class can also be used as a context manager, in which case it will cancel the periodic task once you exit the context.

set_period(period: Union[timedelta, float]) None[source]

Change the period of the operation.

cancel() None[source]

Cancel the periodic operation, and wait until the thread has joined.

If an operation is currently active, this will wait until it finishes.

run() None[source]

Implementation of the periodic thread.