Last active
March 7, 2021 21:36
-
-
Save pat123456/2e2fccbae13116d9eead39e6dcaa7a61 to your computer and use it in GitHub Desktop.
async
This file contains 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 | |
import time | |
async def say_after(delay, what): | |
await asyncio.sleep(delay) | |
print(what) | |
async def main(): | |
task1 = asyncio.create_task( | |
say_after(1, ‘hello’)) | |
task2 = asyncio.create_task( | |
say_after(2, ‘world’)) | |
print(f »started at {time.strftime(‘%X’)} ») | |
# Wait until both tasks are completed (should take | |
# around 2 seconds.) | |
await task1 | |
await task2 | |
print(f »finished at {time.strftime(‘%X’)} ») | |
asyncio.run(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment