Created
June 18, 2018 08:35
-
-
Save JeremyEnglert/ef897f51f63a40b4bab955960f0b1f75 to your computer and use it in GitHub Desktop.
Asyncio Python
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
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