Ian Kelly added the comment:

Fair enough. I think there should be some documentation though to the effect 
that coroutines aren't robust to passing StopIteration across coroutine 
boundaries. It's particularly surprising with PEP-492 coroutines, since those 
aren't even iterators and intuitively should ignore StopIteration like normal 
functions do.

As it happens, this variation (moving the try-except into the executor thread) 
does turn out to work but is probably best avoided for the same reason. I don't 
think it's obviously bad code though:


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):
        def _next(iterator):
            try:
                return next(iterator)
            except StopIteration:
                raise StopAsyncIteration
        return await self._loop.run_in_executor(
                self._executor, _next, self._iterator)

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue26221>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to