Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Oscar Benjamin Newsgroups: comp.lang.python Subject: Re: Cannot step through asynchronous iterator manually Date: Sat, 30 Jan 2016 14:11:40 +0000 Lines: 50 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de lwcvK5isrFkCit4uJrAh+w9xMd9vtnD9Fkv3H3+AjL9A== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'elif': 0.04; 'base.': 0.05; 'cc:addr:python-list': 0.09; 'rows': 0.09; 'thread': 0.10; 'syntax': 0.13; 'def': 0.13; 'appropriate': 0.14; '2))': 0.16; '2016': 0.16; 'async': 0.16; 'cc:name:python list': 0.16; 'consume': 0.16; 'iterator': 0.16; 'itertools': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'row': 0.16; 'skip:n 70': 0.16; 'wrote:': 0.16; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'posted': 0.21; 'wrote': 0.23; "haven't": 0.24; 'header:In- Reply-To:1': 0.24; 'example': 0.26; 'message-id:@mail.gmail.com': 0.27; 'equivalent,': 0.29; 'idea,': 0.29; 'pep': 0.29; 'code': 0.30; 'class': 0.33; 'except': 0.34; 'received:google.com': 0.35; 'item': 0.35; 'but': 0.36; 'too': 0.36; 'should': 0.36; 'there': 0.36; 'received:209.85': 0.36; 'subject:: ': 0.37; 'wanted': 0.37; 'january': 0.38; 'received:209': 0.38; 'end': 0.39; 'your': 0.60; 'back': 0.62; 'more': 0.63; 'realise': 0.66; 'frank': 0.72; 'await': 0.76; 'square': 0.76; 'oscar': 0.84; 'received:209.85.215.42': 0.84; 'improvement': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=hgqAFMhNWF6H6pNwIk6R74aVtl8gzLeaTBcDOmRlYGM=; b=RY7yDpxWJtop3kJfs/TDmV2ac4+GitRlh1uZ8qV/qOwqBqQK51t4oNae9x8Fcr9ja/ KZ/0mwIvlNeBE1ofRDUM1arssx/9fpwH7CCXn1peZiCEcHWSuJOr26twf58yDA7bp1/U IG3ISYeGkxx51nPqXjcWnLbaAT38M/zc1TKd5j5flIsu/GVr4E+4+o2IpNL5rQDFdBYU EVd5w7tWEwOn8pcF4nD041W3eqawhpeltiuCqLh3vEZvHX/qXSb71GjULWRQw7bM+zts T4pVbmL99RdEfWPoQE3mjCAvIqvurmxuCC/IpfMYPv7dJT0ort1XWU2eN35yIxH4X0NY Fsvg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc:content-type; bh=hgqAFMhNWF6H6pNwIk6R74aVtl8gzLeaTBcDOmRlYGM=; b=YA50sM84FSo3sJqumhFwE30ZWO8ipLV2uKsIQBiy65JlNKXBZ3Sa3oCuQzVp9hpygO 7w3swkuU6THsFT72iSBD5HTne3WlBOl6f/Hzgdk0rUu2XAC92mnpOXtEe9GGOPa6CAPi QGYQghcTNYdUxzNL9WKWPxqSdCJYgozDGKDV0Q8zbOlm7d4aPLQlgqZm8mZ2fELV5juI mVC8Da0p4fr4kYC612I1TuZwVvEZSFsqIuBsNgFDNqXH3yy8eKZv1EnHYdZdK2tr3z34 JwyMTZ8sIXr2N5Blkf5kA6K2Jn+RMQqqNLO8k905sslqYJbxgzA1/jRutRNTMXZNN7Lq iUSQ== X-Gm-Message-State: AG10YOQEHcFRsS/x21pap1TNirJET+CQUHADEK5AYRtZdAvqBvqbhHKdcd+SH9zfhiii6k/QCOVKo27//vP+bQ== X-Received: by 10.25.16.30 with SMTP id f30mr5710661lfi.126.1454163120500; Sat, 30 Jan 2016 06:12:00 -0800 (PST) In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:102305 On 30 January 2016 at 13:45, Frank Millman 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