Created
January 22, 2017 00:03
-
-
Save t2y/f615a7e648dcaca1e415973c69e6cb88 to your computer and use it in GitHub Desktop.
async for and async iterable example
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
# -*- coding: utf-8 -*- | |
""" | |
pep492 aiter using queue before pep 525 | |
https://www.python.org/dev/peps/pep-0492 | |
https://www.python.org/dev/peps/pep-0525 | |
""" | |
import asyncio | |
async def yield_num(num, queue): | |
for i in range(num): | |
queue.put_nowait(i) | |
await asyncio.sleep(0.5) | |
class AsyncIterable: | |
def __init__(self, queue): | |
self.queue = queue | |
self.done = [] | |
def __aiter__(self): | |
return self | |
async def __anext__(self): | |
data = await self.fetch_data() | |
if data is not None: | |
return data | |
else: | |
raise StopAsyncIteration | |
async def fetch_data(self): | |
while not self.queue.empty(): | |
self.done.append(self.queue.get_nowait()) | |
if not self.done: | |
return None | |
return self.done.pop(0) | |
async def consume_num(queue): | |
async for i in AsyncIterable(queue): | |
await asyncio.sleep(0.5) | |
print(i) | |
def main(): | |
event_loop = asyncio.get_event_loop() | |
queue = asyncio.Queue(loop=event_loop) | |
try: | |
event_loop.create_task(yield_num(10, queue)) | |
event_loop.run_until_complete(consume_num(queue)) | |
finally: | |
event_loop.close() | |
if __name__ == '__main__': | |
main() |
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
# -*- coding: utf-8 -*- | |
""" | |
pep 525 -- asynchronous generators | |
https://www.python.org/dev/peps/pep-0525 | |
""" | |
import asyncio | |
async def aiter(num): | |
for i in range(num): | |
await asyncio.sleep(0.5) | |
yield i | |
async def run(num): | |
async for i in aiter(num): | |
print(i) | |
def main(): | |
event_loop = asyncio.get_event_loop() | |
try: | |
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