Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'elif': 0.05; 'pep': 0.07; 'subject:search': 0.07; "(i'm": 0.09; 'none:': 0.09; 'this:': 0.11; 'def': 0.13; 'am,': 0.14; 'wrote:': 0.14; 'iterator,': 0.16; 'result)': 0.16; 'sane': 0.16; 'sequence.': 0.16; 'long,': 0.16; 'this?': 0.18; 'method.': 0.19; 'occurred': 0.19; 'tue,': 0.20; 'work,': 0.20; 'cheers,': 0.20; 'code': 0.22; 'header:In-Reply-To:1': 0.22; 'file,': 0.22; 'itself.': 0.22; 'item.': 0.23; 'wonder': 0.24; 'guess': 0.26; 'received:209.85.161.46': 0.26; 'received:mail- fx0-f46.google.com': 0.26; 'chris': 0.27; 'message- id:@mail.gmail.com': 0.28; 'raise': 0.29; 'received:209.85.161': 0.29; "won't": 0.30; '17,': 0.31; 'none,': 0.31; 'to:addr:python- list': 0.32; 'wanting': 0.33; 'break': 0.33; 'like:': 0.35; 'none': 0.36; 'two': 0.37; 'case': 0.37; 'received:209.85': 0.37; 'run': 0.37; 'feels': 0.38; 'sequence': 0.38; 'received:google.com': 0.38; 'but': 0.38; 'anything': 0.38; 'to.': 0.39; 'provider': 0.39; 'to:addr:python.org': 0.39; 'could': 0.39; 'comes': 0.39; 'received:209': 0.39; 'how': 0.39; 'would': 0.40; 'header:Received:5': 0.40; 'might': 0.40; '8bit%:4': 0.60; 'back': 0.61; '2011': 0.62; '11:04': 0.84; 'yielded': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:from:date :message-id:subject:to:content-type:content-transfer-encoding; bh=5uV10fi6Jm6E7Gk9c4Y3hwpNRtM0EdLCEh56+Egg0uc=; b=qGSNz7Dq8h3UEhCDWNUXtQaRFYP9n78AHOR90hCzWh82rsCHBhEF8TlZtC67l5w0aW n96wtBV4FNvXcowFo4mpYpbYwZMVVoHOFsPRuZFRHN6Cdq6ibYhBH+Hh8KDKkbuLCle+ mXKD/ejMk7h8jOOf5npr1tCWqUW/XdUmglwbg= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type:content-transfer-encoding; b=ojenQEG6D5S/IAdC8lv99BcSwOCgtNobKcffA3LrIIiit49bRHBPjcKsLdt+scGhWG W8xQ4Aq9NCNk1r9zR+iGBQMUjDZyTRByXt2rkYZkyLwSplRrF+IAua8vgNxuBCnKsTCP A4IvtDpFuMEH8CpJ8TNl1107TPYuF50hvieTw= MIME-Version: 1.0 In-Reply-To: <4DD2AA99.3010508@simplistix.co.uk> References: <4DD2AA99.3010508@simplistix.co.uk> From: Ian Kelly Date: Tue, 17 May 2011 11:26:54 -0600 Subject: Re: in search of graceful co-routines To: Python Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 56 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1305653245 news.xs4all.nl 49180 [::ffff:82.94.164.166]:41714 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:5584 On Tue, May 17, 2011 at 11:04 AM, Chris Withers wr= ote: > Now, since the sequence is long, and comes from a file, I wanted the > provider to be an iterator, so it occurred to me I could try and use the = new > 2-way generator communication to solve the "communicate back with the > provider", with something like: > > for item in provider: > =A0try: > =A0 =A0consumer.handleItem(self) > =A0except: > =A0 =A0 provider.send('fail') > =A0else: > =A0 =A0 provider.send('succeed') > > ..but of course, this won't work, as 'send' causes the provider iteration= to > continue and then returns a value itself. That feels weird and wrong to m= e, > but I guess my use case might not be what was intended for the send metho= d. > > Anyway, I wonder how other people would write this? > (I'm particularly interested in a sane way to use the two way communicati= on > that PEP 342 introduced) You can use send the way you're wanting to. It will look something like th= is: def provider(): result =3D None while True: if result is None: if has_more_items(): next_item =3D get_next_item() else: break elif result =3D=3D 'fail': process_fail() next_item =3D None elif result =3D=3D 'succeed': process_succeed() next_item =3D None else: raise ValueError('unknown result %s' % result) result =3D (yield next_item) Whenever you call provider().next() or provider().send(None), you simply get the next item. If you call provider().send('succeed') or provider().send('fail'), then the corresponding code is run and the yielded value is None, without consuming anything from the sequence. Cheers, Ian