Skip to content

Instantly share code, notes, and snippets.

@khardix
Created October 5, 2017 07:28
Show Gist options
  • Save khardix/92605f24e0cb2233029d27eda1097078 to your computer and use it in GitHub Desktop.
Save khardix/92605f24e0cb2233029d27eda1097078 to your computer and use it in GitHub Desktop.
AsyncIO experimentation
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