Asyncio Note

Starting async function without await

Async function in asyncio cannot start directly. It should be wrapped in asyncio.create_task.

The asyncio.ensure_future can also be used. But

create_task() (added in Python 3.7) is the preferable way for spawning new tasks.

Call usual function asynchronously with event loop

  • start on next tick: loop.call_soon(func)
  • delay for some time before start: loop.call_later(delay, func)
  • start at a time: loop.call_at(when, func)

See in asyncio event loop.

To await a usual function by returning Future object

Using asyncio.add_done_callback can help.

But I am not sure what scene we need it instead of await async_func(). Maybe the comment here can provide some help.

Reference