Re: peak.events.activity

"P.J. Eby" <pje-Wh6+Hckhi6HFNGf7iClzIwC/[email protected]> Thu, 05 Mar 2009 00:00:15 -0500
Newsgroups gmane.comp.python.peak
Message-ID <[email protected]>
At 09:41 PM 3/4/2009 -0500, Andrew Svetlov wrote:
>It will be great if you add to Activity.txt example of receiving
>values from subtasks in more clean way.
>
>Something very simple, like
>
>from peak.events import trellis, activity
>
>
>class TaskExample(trellis.Component):
>     trellis.attrs(
>         start = False,
>         stop = False
>     )
>
>     def wait_for_start(self):
>         i = 0
>         while not self.start:
>             yield "waiting to start %i"%i
>             yield activity.Pause
>             i += 1
>
>     def wait_for_stop(self):
>         i = 0
>         while not self.stop:
>             yield "waiting to stop %i"%i
>             yield activity.Pause
>
>
>     @activity.task
>     def demo(self):
>         for i in self.wait_for_start():
>             print i
>             yield i
>         print "starting"
>         for i in self.wait_for_stop():
>             print i
>             yield i
>         print "stopped"

The above should read:

    yield self.wait_for_start()
    yield self.wait_for_stop()

There is no need to loop over the results.


>It's obvious for me now: when I want to get result from subtask I need
>to iterate over and pass all unexpected results to upcoming stream.
>But it was not obvious after documentation reading.
>
>I'm wrong?

Yes.  ;-)  It's even easier than that, you just yield the invocation; 
you don't loop over it.

By the way, please send your emails to the PEAK mailing list in 
future, not to me directly.  The mailing list address is shown as the 
"maintainer" email on my packages for a reason.