2 ms·
Not just that, it is also annoying to work around. Sometimes you really do want to kick off a background task and have it run to completion, even if you can't k
by remram 4y ago
Not just that, it is also annoying to work around. Sometimes you really do want to kick off a background task and have it run to completion, even if you can't keep a handle on it, just like a thread. This is surprisingly hard to do.
- vore 4y agoYou 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.