Skip to content

Instantly share code, notes, and snippets.

@databasedav
Created February 18, 2020 21:42
Show Gist options
  • Save databasedav/105883e93c32bac6afbb932f8587460d to your computer and use it in GitHub Desktop.
Save databasedav/105883e93c32bac6afbb932f8587460d to your computer and use it in GitHub Desktop.
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)
await function(*args, **kwargs)
return create_task(task())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment