title: PyCon 2012 Talk by David Mertz on Coroutines as Threads
body: <p>In this <a href="https://www.youtube.com/watch?v=b7R3-_ViNxk&amp;t=1398" rel="nofollow noreferrer">talk</a> by David Mertz on Coroutines, Threads etc. from PyCon 2012, he shows an example of using coroutines(generators) as a replacement for threads:</p>
  
  <p>Here is the slide content:</p>
  
  <blockquote>
    <p>A scheduler for weightless threads won't switch to a specific place,
    but will round-robin or the like:</p>
  </blockquote>
  
  <pre class="lang-py prettyprint-override"><code>def thread_scheduler(generators):
      thread_data = defaultdict(list)
      threads = [g() for g in generators()]
      while True:
          try:
              for t in threads:
                  data = thread_data[t.__name__]
                  consumer, data = t.send(data)
                  thread_data[consumer].append(data)
          except StopIteration:
              break
  </code></pre>
  
  <blockquote>
    <p>Here consumer threads cannot be switched into directly, but once
    reached will get injected data.</p>
  </blockquote>
  
  <p>1) Is <code>generators()</code> a typo? Most likely <code>generator</code> is a list right? (I know you can write function that gives you some iterable that has generators in it; but for simplicity ...)</p>
  
  <p>2) Why is the code catching only <em>one</em> <code>StopIeration</code> error when there are so many generators being looped? </p>