Created
April 28, 2019 20:49
-
-
Save doron2402/3a42af2da09b4f2c005f22cbe3b4bb0b to your computer and use it in GitHub Desktop.
python asyncio vs sync code
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
#!/usr/bin/env python3 | |
import asyncio | |
import time | |
async def count(): | |
print("One") | |
await asyncio.sleep(1) | |
print("Two") | |
def count_sync(): | |
print("One (Sync)") | |
time.sleep(1) | |
print("Two (Sync)") | |
async def main_async(): | |
await asyncio.gather(count(), count(), count()) | |
def main_sync(): | |
count_sync() | |
count_sync() | |
count_sync() | |
if __name__ == "__main__": | |
# async | |
s = time.perf_counter() | |
asyncio.run(main_async()) | |
elapsed = time.perf_counter() - s | |
print(f"{__file__} executed in {elapsed:0.2f} seconds.") | |
# sync | |
s = time.perf_counter() | |
asyncio.run(main_sync()) | |
elapsed = time.perf_counter() - s | |
print(f"{__file__} executed in {elapsed:0.2f} seconds.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment