Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save normanlmfung/95bacafd214a26b3af85c6bd02c6c329 to your computer and use it in GitHub Desktop.
Save normanlmfung/95bacafd214a26b3af85c6bd02c6c329 to your computer and use it in GitHub Desktop.
python_syntax_asyncio_create_task_fireforget
import asyncio
from datetime import datetime
def _log(msg : str):
print(f"{datetime.utcnow()} {msg}")
async def dummy(name, delay_sec):
_log(f"{name} entering ...")
await asyncio.sleep(delay_sec)
_log(f"{name} done for the day!")
# https://stackoverflow.com/questions/44630676/how-can-i-call-an-async-function-without-await/73759454#73759454
async def main():
asyncio.create_task(dummy('dummy1', 5)) # last to finish
asyncio.create_task(dummy('dummy2', 3)) # 2nd
asyncio.create_task(dummy('dummy3', 1)) # First to finish
_log(f"Yo I didn't wait for ya!")
await asyncio.sleep(10)
asyncio.get_event_loop().run_until_complete(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment