Skip to content

Instantly share code, notes, and snippets.

@jayzhou215
Last active November 29, 2019 02:23
Show Gist options
  • Save jayzhou215/074231ef654a487079c51e96e1346240 to your computer and use it in GitHub Desktop.
Save jayzhou215/074231ef654a487079c51e96e1346240 to your computer and use it in GitHub Desktop.
import asyncio
import random
import time
async def do_stuff(i):
ran = random.uniform(1, 5)
await asyncio.sleep(ran) # NOTE if we hadn't called
# asyncio.set_event_loop() earlier, we would have to pass an event
# loop to this function explicitly.
print(i, time.time(), ran)
return ran
async def main():
now = time.time()
futures = [do_stuff(i) for i in range(10000)]
await asyncio.gather(
*futures
)
print(time.time() - now)
if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment