title: python generators for concurrency
body: <p>I am following slides from Python's guru David Beazley. It states "Generators are also used for concurrency. Here is an example:</p>
  
  <pre><code>from collections import deque
  
  def countdown(n):
      while n &gt; 0:
          print("T-minus", n)
          yield
          n -=1
  
  def countup(n):
      x = 0
      while x &gt; n:
          print("Up we go", x)
          yield
          x +=1
  
  # instantiate some tasks in a queue
  tasks = deque([countdown(10),
                 countdown(5),
                 countup(20)
                 ])
  
  # run a little scheduler
  while tasks:
      t = tasks.pop()  # get a task
      try:
          next(t)   # run it until it yields
          tasks.appendleft(t) # reschedule
      except StopIteration:
          pass
  </code></pre>
  
  <p>Here is the output:</p>
  
  <pre><code>T-minus 5
  T-minus 10
  T-minus 4
  T-minus 9
  T-minus 3
  T-minus 8
  T-minus 2
  T-minus 7
  T-minus 1
  T-minus 6
  T-minus 5
  T-minus 4
  T-minus 3
  T-minus 2
  T-minus 1
  </code></pre>
  
  <p>The question is how is concurrency introduced by generators and how is it manifesting?</p>