Skip to content

Instantly share code, notes, and snippets.

@t2y
Created January 22, 2017 00:27
Show Gist options
  • Save t2y/ebc5ebc7e311b872400c9f4fc0aaf54d to your computer and use it in GitHub Desktop.
Save t2y/ebc5ebc7e311b872400c9f4fc0aaf54d to your computer and use it in GitHub Desktop.
asynchronous comprehensions
# -*- coding: utf-8 -*-
"""
pep 530 -- asynchronous comprehensions
https://www.python.org/dev/peps/pep-0530
"""
import asyncio
from random import choice
async def do_something():
while True:
await asyncio.sleep(0.2)
print('do something ...')
async def aiter(num):
for i in range(num):
await asyncio.sleep(0.1)
print('%d yielding aiter ...' % i)
yield i
async def apow(num):
sleep_second = choice(range(3))
await asyncio.sleep(sleep_second)
print('%d applying apow ...' % num)
return pow(num, 2)
async def run(num):
data = [await apow(i) async for i in aiter(num)]
for i in data:
print(i)
def main():
event_loop = asyncio.get_event_loop()
try:
event_loop.create_task(do_something())
event_loop.run_until_complete(run(10))
finally:
event_loop.close()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment