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


Groups > comp.lang.python > #102164 > unrolled thread

Re: Question about asyncio and blocking operations

Started byIan Kelly <ian.g.kelly@gmail.com>
First post2016-01-27 10:14 -0700
Last post2016-01-27 10:14 -0700
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

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

#102164 — Re: Question about asyncio and blocking operations

FromIan Kelly <ian.g.kelly@gmail.com>
Date2016-01-27 10:14 -0700
SubjectRe: Question about asyncio and blocking operations
Message-ID<mailman.39.1453914901.2338.python-list@python.org>
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.

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web