Created
June 23, 2017 04:33
-
-
Save dputtick/c76039b11f86b1a58c458321a5bbac20 to your computer and use it in GitHub Desktop.
Asyncio counting
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 | |
async def count(name, number): | |
n = 1 | |
while n < number + 1: | |
print(name, ':', n) | |
n += 1 | |
await asyncio.sleep(1) | |
return n | |
async def run(): | |
coros = [] | |
for name in range(10): | |
coros.append(count(name, 10)) | |
numbers = await asyncio.wait(coros) | |
return numbers | |
def main(): | |
loop = asyncio.get_event_loop() | |
numbers = loop.run_until_complete(run()) | |
print(numbers) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment