Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is a <a href="http://www.dabeaz.com/coroutines/coroutine.py" rel="noreferrer">basic example of a coroutine</a>:</p> <pre><code>def coroutine(func): def start(*args,**kwargs): cr = func(*args,**kwargs) cr.next() return cr return start @coroutine def grep(pattern): print "Looking for %s" % pattern while True: line = (yield) if pattern in line: print(line) g = grep("python") # Notice how you don't need a next() call here g.send("Yeah, but no, but yeah, but no") g.send("A series of tubes") g.send("python generators rock!") # Looking for python # python generators rock! </code></pre> <p>We can make a class which contains such a coroutine, and delegates calls to its <code>send</code> method to the coroutine:</p> <pre><code>class Foo(object): def __init__(self,pattern): self.count=1 self.pattern=pattern self.grep=self._grep() @coroutine def _grep(self): while True: line = (yield) if self.pattern in line: print(self.count, line) self.count+=1 def send(self,arg): self.grep.send(arg) foo = Foo("python") foo.send("Yeah, but no, but yeah, but no") foo.send("A series of tubes") foo.send("python generators rock!") foo.pattern='spam' foo.send("Some cheese?") foo.send("More spam?") # (1, 'python generators rock!') # (2, 'More spam?') </code></pre> <p>Notice that <code>foo</code> acts like a coroutine (insofar as it has a send method), but is a class -- it can have attributes and methods which can interact with the coroutine.</p> <p>For more information (and wonderful examples), see David Beazley's <a href="http://www.dabeaz.com/coroutines/index.html" rel="noreferrer">Curious Course on Coroutines and Concurrency.</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload