Created
October 5, 2017 07:28
-
-
Save khardix/92605f24e0cb2233029d27eda1097078 to your computer and use it in GitHub Desktop.
AsyncIO experimentation
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 | |
async def square(stream): | |
async for num in stream: | |
yield num ** 2 | |
async def add_two(stream): | |
async for num in stream: | |
yield num + 2 | |
async def arange(*range_args): | |
for x in range(*range_args): | |
await asyncio.sleep(random.uniform(0.1, 0.4)) | |
yield x | |
async def main(): | |
values = arange(5) | |
values = add_two(values) | |
values = square(values) | |
return [val async for val in values] | |
if __name__ == '__main__': | |
loop = asyncio.get_event_loop() | |
res = loop.run_until_complete(main()) | |
print('Result:', res) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment