Skip to content

Instantly share code, notes, and snippets.

@onlined
Created March 10, 2023 02:01
Show Gist options
  • Save onlined/8beda042ac22ef6e9451743e79486b03 to your computer and use it in GitHub Desktop.
Save onlined/8beda042ac22ef6e9451743e79486b03 to your computer and use it in GitHub Desktop.
Easier and secure "fire and forget" for async tasks
import asyncio
def _create_fire_and_forget():
tasks = set()
def fire_and_forget(*args, **kwargs):
task = asyncio.create_task(*args, **kwargs)
tasks.add(task)
task.add_done_callback(tasks.discard)
return task
return fire_and_forget
fire_and_forget = _create_fire_and_forget()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment