Skip to content

Instantly share code, notes, and snippets.

@pat123456
Last active March 7, 2021 21:36
Show Gist options
  • Save pat123456/2e2fccbae13116d9eead39e6dcaa7a61 to your computer and use it in GitHub Desktop.
Save pat123456/2e2fccbae13116d9eead39e6dcaa7a61 to your computer and use it in GitHub Desktop.
async
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