Quantcast
Channel: How can I call an async function without await? - Stack Overflow
Viewing all articles
Browse latest Browse all 7

Answer by user3761555 for How can I call an async function without await?

$
0
0

In simplest form:

import asynciofrom datetime import datetimedef _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!")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())

Output:

2022-09-18 00:53:13.428285 Yo I didn't wait for ya!2022-09-18 00:53:13.428285 dummy1 entering ...2022-09-18 00:53:13.428285 dummy2 entering ...2022-09-18 00:53:13.428285 dummy3 entering ...2022-09-18 00:53:14.436801 dummy3 done for the day!2022-09-18 00:53:16.437226 dummy2 done for the day!2022-09-18 00:53:18.424755 dummy1 done for the day!

Viewing all articles
Browse latest Browse all 7

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>