4 ms·
You can always do something extremely goofy like this (but you shouldn't): _running_tasks = set() def my_create_task(coro, **kwargs): async de
by vore 4y ago
You can always do something extremely goofy like this (but you shouldn't):
_running_tasks = set()
def my_create_task(coro, **kwargs):
async def _coro():
nonlocal task
try:
return await coro
finally:
_running_tasks.remove(task)
task = asyncio.create_task(_coro(), **kwargs)
_running_tasks.add(task)
return task
- remram 4y agoI have a very similar helper in multiple codebases in production. It has some additional bits to log errors and to handle tasks that should never complete (basically a flag to call sys.exit if they do) but it's basically this. I don't understand why theads get a background mode but tasks can't be fire-and-forget.