Skip to content

Instantly share code, notes, and snippets.

@databasedav
databasedav / spawn_after.py
Created February 18, 2020 21:42
python async spawn after
def spawn_after(seconds, function, *args, **kwargs):
"""
Calls function with args and kwargs after seconds.
Runs in background.
Function must be a coroutine function.
Returns task object which can be cancelled, waited for, etc.
Named after Eventlet function https://eventlet.net/doc/modules/greenthread.html#eventlet.greenthread.spawn_after.
"""
async def task():
await sleep(seconds)