Created
May 12, 2021 13:51
-
-
Save xnuinside/9e3115ac81b0ae4616ee8cd1142f8231 to your computer and use it in GitHub Desktop.
Background Task sample
This file contains hidden or 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
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