scheduling#
Generic python scheduler.
- class Scheduler(name)[source]#
Bases:
objectSchedule the execution of coroutines and keep track of them.
When instantiating a
Scheduler, a name must be provided. This name is used to distinguish the instance’s log messages from other instances. Using the name of the class or module containing the instance is suggested.Coroutines can be scheduled immediately with
scheduleor in the future withschedule_atorschedule_later. A unique ID is required to be given in order to keep track of the resulting Tasks. Any scheduled task can be cancelled prematurely usingcancelby providing the same ID used to schedule it.The
inoperator is supported for checking if a task with a given ID is currently scheduled.Any exception raised in a scheduled task is logged when the task is done.
- cancel(task_id)[source]#
Unschedule the task identified by
task_id. Log a warning if the task doesn’t exist.
- schedule(task_id, coroutine)[source]#
Schedule the execution of a
coroutine.If a task with
task_idalready exists, closecoroutineinstead of scheduling it. This prevents unawaited coroutine warnings. Don’t pass a coroutine that’ll be re-used elsewhere.
- schedule_at(time, task_id, coroutine)[source]#
Schedule
coroutineto be executed at the giventime.If
timeis timezone aware, then use that timezone to calculate now() when subtracting. Iftimeis naïve, then use UTC.If
timeis in the past, schedulecoroutineimmediately.If a task with
task_idalready exists, closecoroutineinstead of scheduling it. This prevents unawaited coroutine warnings. Don’t pass a coroutine that’ll be re-used elsewhere.
- create_task(coro, *, suppressed_exceptions=(), event_loop=None, **kwargs)[source]#
Wrapper for creating an
asyncio.Taskwhich logs exceptions raised in the task.If the
event_loopkwarg is provided, the task is created from that event loop, otherwise the running loop is used.- Parameters:
coro (
Coroutine[Any,Any,TypeVar(TASK_RETURN)]) – The function to call.suppressed_exceptions (
tuple[type[Exception]]) – Exceptions to be handled by the task.event_loop (
asyncio.AbstractEventLoop) – The loop to create the task from.kwargs – Passed to
asyncio.create_task().
- Returns:
The wrapped task.
- Return type: