Last active
April 25, 2019 11:48
-
-
Save Glyphack/90eae68e9006344ce0e851a4fc1e4db0 to your computer and use it in GitHub Desktop.
Implementing sorting with async.sleep
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 time | |
import random | |
async def task(i): | |
await asyncio.sleep(i* 0.01) | |
print(i) | |
async def main(): | |
a = [random.randint(0, 100) for _ in range(0, 100)] | |
coroutines = list() | |
for i in a: | |
coroutines.append(task(i)) | |
await asyncio.gather(*coroutines) | |
start = time.time() | |
asyncio.run(main()) | |
print(f"program took {time.time() - start} to finish") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment