Question about asyncio and blocking operations

Ian Kelly ian.g.kelly at gmail.com
Wed Jan 27 12:14:13 EST 2016


On Wed, Jan 27, 2016 at 9:15 AM, Ian Kelly <ian.g.kelly at gmail.com> wrote:
> class CursorWrapper:
>
>     def __init__(self, cursor):
>         self._cursor = cursor
>
>     async def __aiter__(self):
>         return self
>
>     async def __anext__(self):
>         loop = asyncio.get_event_loop()
>         return await loop.run_in_executor(None, next, self._cursor)

Oh, except you'd want to be sure to catch StopIteration and raise
AsyncStopIteration in its place. This could also be generalized as an
iterator wrapper, similar to the example in the PEP except using
run_in_executor to actually avoid blocking.

class AsyncIteratorWrapper:

    def __init__(self, iterable, loop=None, executor=None):
        self._iterator = iter(iterable)
        self._loop = loop or asyncio.get_event_loop()
        self._executor = executor

    async def __aiter__(self):
        return self

    async def __anext__(self):
        try:
            return await self._loop.run_in_executor(
                    self._executor, next, self._iterator)
        except StopIteration:
            raise StopAsyncIteration


Unfortunately this doesn't actually work at present.
EventLoop.run_in_executor swallows the StopIteration exception and
just returns None, which I assume is a bug.



More information about the Python-list mailing list