Skip to content

Clarify that PeriodicCallback accepts async functions #3117

@crusaderky

Description

@crusaderky

The type annotations of PeriodicCallback state callback: Callable[[], None]. The documentation does not clarify.
Both the annotations and the docstring should clarify that both sync and async functions, as well as sync functions that return Futures, are valid callbacks (callback: Callable[[], None] | Callable[[], Awaitable[None]).

However, this note in the docstring:

    If the callback runs for longer than ``callback_time`` milliseconds,
    subsequent invocations will be skipped to get back on schedule.

only seems to apply to sync functions; this should either be fixed or clarified in the docs.
The code below prints "foo" every 1 second instead of 3:

async def foo():
    await asyncio.sleep(3)
    print("foo")

pc = PeriodicCallback(foo, 1000)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions