Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save JeremyEnglert/ef897f51f63a40b4bab955960f0b1f75 to your computer and use it in GitHub Desktop.
Save JeremyEnglert/ef897f51f63a40b4bab955960f0b1f75 to your computer and use it in GitHub Desktop.
Asyncio Python
import asyncio
import random
import time
async def myCoroutine(id):
process_time = random.randint(1,5)
await asyncio.sleep(process_time)
print("Coroutine {}, has finished after {} seconds.".format(id, process_time))
async def main():
start = time.time()
tasks = []
for i in range(10):
tasks.append(asyncio.ensure_future(myCoroutine(i)))
await asyncio.gather(*tasks)
end = time.time()
print("This task took " + str(end - start) + " seconds to complete.")
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment