Skip to content

Instantly share code, notes, and snippets.

@xnuinside
Created May 12, 2021 13:51
Show Gist options
  • Save xnuinside/9e3115ac81b0ae4616ee8cd1142f8231 to your computer and use it in GitHub Desktop.
Save xnuinside/9e3115ac81b0ae4616ee8cd1142f8231 to your computer and use it in GitHub Desktop.
Background Task sample
import asyncio
from fastapi import BackgroundTasks
cur_folder = os.path.dirname(os.path.abspath(__file__))
def write_notification(email: str, message=""):
with open(os.path.join(cur_folder, "log.txt"), mode="w+") as email_file:
content = f"notification for {email}: {message}"
email_file.write(content)
background_tasks = BackgroundTasks()
email = 'any str'
background_tasks.add_task(write_notification, email, message="some notification")
asyncio.get_event_loop().run_until_complete(background_tasks())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment