Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #102164

Re: Question about asyncio and blocking operations

From Ian Kelly <ian.g.kelly@gmail.com>
Newsgroups comp.lang.python
Subject Re: Question about asyncio and blocking operations
Date 2016-01-27 10:14 -0700
Message-ID <mailman.39.1453914901.2338.python-list@python.org> (permalink)
References <n8038j$575$1@ger.gmane.org> <n8818q$35e$1@ger.gmane.org> <CALwzidk-RBkB-vi6CgcEeoFHQrsoTFvqX9MqzDD=rnY5bOCRUg@mail.gmail.com> <n8aln3$fah$1@ger.gmane.org> <CALwzidn6TvN9W-2qnn2JYvJu8NHzn499nPtfjn9OHjdDcebVbA@mail.gmail.com>

Show all headers | View raw


On Wed, Jan 27, 2016 at 9:15 AM, Ian Kelly <ian.g.kelly@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.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Question about asyncio and blocking operations Ian Kelly <ian.g.kelly@gmail.com> - 2016-01-27 10:14 -0700

csiph-web