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


Groups > comp.lang.python > #5582

in search of graceful co-routines

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeder.news-service.com!xlned.com!feeder5.xlned.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <chris@simplistix.co.uk>
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; 'pep': 0.07; 'subject:search': 0.07; 'python': 0.07; "(i'm": 0.09; 'from:addr:chris': 0.09; 'to:name:python list': 0.09; 'except:': 0.16; 'from:addr:simplistix.co.uk': 0.16; 'from:name:chris withers': 0.16; 'iterator,': 0.16; 'message-id:@simplistix.co.uk': 0.16; 'received:89.151': 0.16; 'received:89.151.125': 0.16; 'received:89.151.125.140': 0.16; 'received:server1.simplistix.co.uk': 0.16; 'received:simplistix.co.uk': 0.16; 'sane': 0.16; 'sequence,': 0.16; 'long,': 0.16; 'this?': 0.18; "hasn't": 0.19; 'method.': 0.19; 'occurred': 0.19; 'work,': 0.20; 'cheers,': 0.20; 'code': 0.22; 'file,': 0.22; 'itself.': 0.22; 'controlling': 0.23; 'wonder': 0.24; 'guess': 0.26; "i'm": 0.26; 'chris': 0.27; "won't": 0.30; 'pattern': 0.31; 'all,': 0.31; 'to:addr:python- list': 0.32; 'received:192.168.1': 0.34; 'received:192': 0.34; 'header:User-Agent:1': 0.35; 'like:': 0.35; 'succeeded': 0.35; 'try:': 0.35; 'url:uk': 0.36; 'processing': 0.37; 'received:192.168': 0.37; 'two': 0.37; 'case': 0.37; 'element': 0.38; 'feels': 0.38; 'sequence': 0.38; 'but': 0.38; 'so,': 0.38; 'signal': 0.39; 'provider': 0.39; 'to:addr:python.org': 0.39; 'could': 0.39; 'comes': 0.39; 'where': 0.39; 'how': 0.39; 'would': 0.40; 'might': 0.40; 'back': 0.61; 'received:89': 0.64; 'batch': 0.69; 'consumer': 0.80
Date Tue, 17 May 2011 18:04:25 +0100
From Chris Withers <chris@simplistix.co.uk>
User-Agent Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2.17) Gecko/20110414 Thunderbird/3.1.10
MIME-Version 1.0
To Python List <python-list@python.org>
Subject in search of graceful co-routines
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
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>
Newsgroups comp.lang.python
Message-ID <mailman.1681.1305651860.9059.python-list@python.org> (permalink)
Lines 44
NNTP-Posting-Host 82.94.164.166
X-Trace 1305651861 news.xs4all.nl 49182 [::ffff:82.94.164.166]:54343
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:5582

Show key headers only | View raw


Hi All,

I'm looking for a graceful pattern for the situation where I have a 
provider of a sequence, the consumer of a sequence and code to moderate 
the two, and where I'd like to consumer to be able to signal to the 
provider that it hasn't succeeded in processing one element in the queue.

So, I'd want the controlling code to look a lot like:

for item in provider:
   try:
     consumer.handleItem(self)
   except:
      provider.failed(item)

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.

Anyway, I wonder how other people would write this?
(I'm particularly interested in a sane way to use the two way 
communication that PEP 342 introduced)

cheers,

Chris

-- 
Simplistix - Content Management, Batch Processing & Python Consulting
            - http://www.simplistix.co.uk

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


Thread

in search of graceful co-routines Chris Withers <chris@simplistix.co.uk> - 2011-05-17 18:04 +0100

csiph-web