Way to run coroutine by datetime

There should way to schedule coroutine execution by datetime.datetime, something like:

asyncio.schedule_task(coroutine, datetime)

so coroutine will be executed in datetime. I think this should very useful feature, with this can replace sleeping until a time and then execute.

It’s there – asyncio.call_at(). Just have to convert from datetime to posix timestamp.

1 Like

I think call_at takes a monotonic time (i.e., “number of seconds the machine has been running”), not a calendar time like a datetime represents?

Handling calender times is unfortunately very complicated, because it’s a totally different timescale than what event loops like asyncio normally use, so you have to handle cases like the machine being suspended (= monotonic time stops ticking, but calendar time keeps ticking), or the user explicitly setting the calendar time to a new value. I think it is possible to implement on top of Linux, macOS, and Windows, using 3 different platform-specific hacks, but asyncio’s regular timing functions alone aren’t enough to do it, and I suspect there are some platforms where Python runs where it’s not possible at all.

Whoops, you’re right. I don’t have anything to add to your suggestion.

1 Like