Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #102305
| From | Oscar Benjamin <oscar.j.benjamin@gmail.com> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: Cannot step through asynchronous iterator manually |
| Date | 2016-01-30 14:11 +0000 |
| Message-ID | <mailman.131.1454163122.2338.python-list@python.org> (permalink) |
| References | <n8hjmt$b8n$1@ger.gmane.org> <CALwzid=sSDSm8hdAN+ORJ54A_jEu9Wc8103iqGKAah8mrj-TXw@mail.gmail.com> <n8hrs1$orn$1@ger.gmane.org> <CAHVvXxSA0Yq4VOYy6qycgXxVpL5zZGM8muUi+1VmeZD8CRgtvg@mail.gmail.com> <n8ieqk$a05$1@ger.gmane.org> |
On 30 January 2016 at 13:45, Frank Millman <frank@chagford.com> wrote:
> "Oscar Benjamin" wrote in message
> news:CAHVvXxSA0Yq4VOYy6qycgXxVpL5zZGM8muUi+1VmeZD8CRgtvg@mail.gmail.com...
>>
>>
>> The simplest thing would just be to call list(cur) but I realise that
>
> you don't want to consume more than 2 rows from the database so just
> use islice:
>>
>>
>> rows = list(islice(cur, 2)) # pull at most 2 rows
>> if not rows:
>> # no rows
>> elif len(rows) > 1:
>> # too many rows
>> row = rows[0]
>>
>
> I like the idea, but I don't think it would work with an asychronous
> iterable.
That was intended as an improvement over the code that you posted for
normal iterators.
> OTOH it should not be difficult to roll your own using the example
> in the itertools docs as a base. Except that the example uses next(it)
> internally, and this thread started with the fact that there is no
> asychronous equivalent, so I might be back to square one.
I haven't used PEP 492 yet but what about:
async def aslice(asynciterator, end):
if end == 0:
return []
items = []
async for item in asynciterator:
items.append(item)
if len(items) == end:
break
return items
rows = await aslice(cur, 2)
AFAICT there's no generator-function-style syntax for writing an async
iterator so you'd have to make a class with the appropriate methods if
you wanted to be able to loop over aslice with async for.
--
Oscar
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Cannot step through asynchronous iterator manually Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2016-01-30 14:11 +0000
csiph-web