Created
February 18, 2020 21:42
-
-
Save databasedav/105883e93c32bac6afbb932f8587460d to your computer and use it in GitHub Desktop.
python async spawn after
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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