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


Groups > comp.lang.python > #102301

Re: Cannot step through asynchronous iterator manually

From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Subject Re: Cannot step through asynchronous iterator manually
Date 2016-01-30 23:55 +1100
Message-ID <mailman.127.1454158530.2338.python-list@python.org> (permalink)
References (2 earlier) <n8hrs1$orn$1@ger.gmane.org> <CAPTjJmoAmVNTCKq7QYaDRNQ67Gcg9TxSXYXCrY==S9Djjna_rA@mail.gmail.com> <n8i4j9$lb4$1@ger.gmane.org> <CAGqiJR8yUdd1u7j0YHS-He_v4uUT-ui=PpiX=n_G=ntt8ZnTpg@mail.gmail.com> <CAKF=+diQQS_FZxHjMwfx1_b1nz7eFdaUbqpUn9nxELC8SHfWpA@mail.gmail.com>

Show all headers | View raw


On Sat, Jan 30, 2016 at 11:35 PM, Kevin Conway
<kevinjacobconway@gmail.com> wrote:
> To address the original question, I don't believe a next() equivalent for
> async iterables has been added to the standard library yet. Here's an
> implementation from one of my projects that I use to manually get the next
> value: https://bpaste.net/show/e4bd209fc067. It exposes the same interface
> as the synchronous next(). Usage:
>
>     await anext(some_async_iterator)
>
> Ultimately, it's a fancy wrapper around the original snippet of 'await
> iterator.__anext__()'.

Curious idiom for the one-or-two-arg situation. Any particular reason
not to use the classic sentinel object model?

_SENTINEL = object()
async def anext(iterable, default=_SENTINEL):
    ...
        if default is not _SENTINEL:
            return default

Or if you want to avoid that, at least take iterable as a fixed arg:

async def anext(iterable, *default):
    if len(default) > 1: TypeError
    ...
        if default: return default[0]

Also curious is that you raise a new StopAsyncIteration from the
original one, rather than just reraising the original. I assume
there's a reason for that, but it doesn't have a comment.

ChrisA

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


Thread

Re: Cannot step through asynchronous iterator manually Chris Angelico <rosuav@gmail.com> - 2016-01-30 23:55 +1100

csiph-web