Skip to content

Instantly share code, notes, and snippets.

@madaniel
Created January 15, 2023 21:08
Show Gist options
  • Save madaniel/f08fa0e99d09750eddd7fa59620b8360 to your computer and use it in GitHub Desktop.
Save madaniel/f08fa0e99d09750eddd7fa59620b8360 to your computer and use it in GitHub Desktop.
Simple async example
import asyncio
import time
# Note - This is python 3.11
async def slow_task(task_id):
await asyncio.sleep(1)
print(f"tick {task_id}\n")
async def main():
await asyncio.wait(
[asyncio.create_task(slow_task(i)) for i in range(1, 4)]
)
if __name__ == "__main__":
# Running async
start = time.perf_counter()
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(main())
async_run = time.perf_counter() - start
print(f"Running in async took {async_run} seconds")
"""
tick 1
tick 2
tick 3
Running in async took 1.0064033749513328 seconds
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment