scheduling¶
Generic python scheduler.
- class Scheduler(name)[source]¶
Bases:
object
Schedule 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
schedule
or in the future withschedule_at
orschedule_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 usingcancel
by providing the same ID used to schedule it.The
in
operator 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_id
already exists, closecoroutine
instead 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
coroutine
to be executed at the giventime
.If
time
is timezone aware, then use that timezone to calculate now() when subtracting. Iftime
is naïve, then use UTC.If
time
is in the past, schedulecoroutine
immediately.If a task with
task_id
already exists, closecoroutine
instead 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.Task
which logs exceptions raised in the task.If the
event_loop
kwarg 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: