Created
August 27, 2019 14:32
-
-
Save TobCap/20a2801bee7598673618e62a4909dda0 to your computer and use it in GitHub Desktop.
python async await example
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 | |
from time import time | |
async def slow_a(): | |
print("a start") | |
await asyncio.sleep(2.5) | |
print("a end") | |
async def slow_b(): | |
print("b start") | |
await asyncio.sleep(2.0) | |
print("b end") | |
async def main(): | |
start = time() | |
print("start main") | |
t1 = asyncio.create_task(slow_a()) | |
t2 = asyncio.create_task(slow_b()) | |
await t1 | |
await t2 | |
print("end main") | |
print(time() - start) | |
if __name__ == '__main__': | |
asyncio.run(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment