Author ikelly
Recipients gvanrossum, haypo, ikelly, yselivanov
Date 2016-01-27.19:36:42
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1453923402.65.0.780452838472.issue26221@psf.upfronthosting.co.za>
In-reply-to
Content
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)
History
Date User Action Args
2016-01-27 19:36:42ikellysetrecipients: + ikelly, gvanrossum, haypo, yselivanov
2016-01-27 19:36:42ikellysetmessageid: <1453923402.65.0.780452838472.issue26221@psf.upfronthosting.co.za>
2016-01-27 19:36:42ikellylinkissue26221 messages
2016-01-27 19:36:42ikellycreate