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


Groups > comp.lang.python > #5586

Re: in search of graceful co-routines

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!selfless.tophat.at!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <pavlovevidence@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.029
X-Spam-Evidence '*H*': 0.94; '*S*': 0.00; 'else:': 0.03; 'subject:search': 0.07; 'wrote:': 0.14; 'cc:name:python list': 0.16; 'except:': 0.16; 'iterator,': 0.16; 'send()': 0.16; 'long,': 0.16; 'method.': 0.19; 'occurred': 0.19; 'cc:2**0': 0.20; 'work,': 0.20; 'header:In-Reply-To:1': 0.22; 'cc:addr:python-list': 0.22; 'file,': 0.22; 'itself.': 0.22; 'loop': 0.22; 'received:209.85.213': 0.23; 'calling': 0.25; 'guess': 0.26; 'chris': 0.27; "won't": 0.30; 'cc:addr:python.org': 0.31; '17,': 0.31; 'carl': 0.31; 'yourself.': 0.33; 'break': 0.33; 'header :User-Agent:1': 0.35; 'like:': 0.35; 'try:': 0.35; 'usually': 0.36; 'none': 0.36; 'case': 0.37; 'should': 0.37; 'received:209.85': 0.37; 'feels': 0.38; 'sequence': 0.38; 'received:google.com': 0.38; 'but': 0.38; 'provider': 0.39; 'could': 0.39; 'comes': 0.39; 'received:209': 0.39; 'except': 0.39; 'might': 0.40; 'back': 0.61; '2011': 0.62; 'to:addr:googlegroups.com': 0.69; 'reply-to:no real name:2**0': 0.72; 'header:Reply-To:1': 0.72; 'hand.': 0.84; 'reply- to:addr:googlegroups.com': 0.93
Newsgroups comp.lang.python
Date Tue, 17 May 2011 10:30:25 -0700 (PDT)
In-Reply-To <mailman.1681.1305651860.9059.python-list@python.org>
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=157.127.155.214; posting-account=XFEWnwoAAADNO10m3Wcmq_SWdmyZuXff
User-Agent G2/1.0
MIME-Version 1.0
Subject Re: in search of graceful co-routines
From Carl Banks <pavlovevidence@gmail.com>
To comp.lang.python@googlegroups.com
Content-Type text/plain; charset=ISO-8859-1
Cc Python List <python-list@python.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
Reply-To comp.lang.python@googlegroups.com
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Message-ID <mailman.1683.1305653427.9059.python-list@python.org> (permalink)
Lines 36
NNTP-Posting-Host 82.94.164.166
X-Trace 1305653427 news.xs4all.nl 49184 [::ffff:82.94.164.166]:39063
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:5586

Show key headers only | View raw


On Tuesday, May 17, 2011 10:04:25 AM UTC-7, Chris Withers wrote:
> 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:
>    try:
>      consumer.handleItem(self)
>    except:
>       provider.send('fail')
>    else:
>       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 me, but I guess my use case might not be what was intended 
> for the send method.

You just have to call send() in a loop yourself.  Note that you should usually catch StopIteration whenever calling send() or next() by hand.  Untested:

result = None
while True:
    try:
        item = provider.send(result)
    except StopIteration:
        break
    try:
        consumer.handleItem(item)
    except:
        result = 'failure'
    else:
        result = 'success'


Carl Banks

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


Thread

Re: in search of graceful co-routines Carl Banks <pavlovevidence@gmail.com> - 2011-05-17 10:30 -0700

csiph-web