Message259049
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) |
|
Date |
User |
Action |
Args |
2016-01-27 19:36:42 | ikelly | set | recipients:
+ ikelly, gvanrossum, vstinner, yselivanov |
2016-01-27 19:36:42 | ikelly | set | messageid: <1453923402.65.0.780452838472.issue26221@psf.upfronthosting.co.za> |
2016-01-27 19:36:42 | ikelly | link | issue26221 messages |
2016-01-27 19:36:42 | ikelly | create | |
|