Skip to content

Instantly share code, notes, and snippets.

@doron2402
Created April 28, 2019 20:49
Show Gist options
  • Save doron2402/3a42af2da09b4f2c005f22cbe3b4bb0b to your computer and use it in GitHub Desktop.
Save doron2402/3a42af2da09b4f2c005f22cbe3b4bb0b to your computer and use it in GitHub Desktop.
python asyncio vs sync code
#!/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