title: Python loop in a coroutine
body: <p>I've read all the documentation on the subject, but it seems I can't grasp the whole concept of Python coroutines well enough to implement what I want to do.</p>
  
  <p>I have a background task (which generates some random files, but that doesn't much matter), and it does this in an infinite loop (this is a watcher).</p>
  
  <p>I would like to implement this background task in the most efficient way possible, and I thought that microthreads (aka coroutines) were a good way to achieve that, but I can't get it to work at all (either it the background task runs or either the rest of the program, but not both at the same time!).</p>
  
  <p>Could someone give me a simple example of a background task implemented using coroutines? Or am I being mistaken in thinking that coroutines could be used for that purpose?</p>
  
  <p>I am using Python 2.7 native coroutines.</p>
  
  <p>I am well versed into concurrency, particularly with DBMSes and Ada, so I know a lot about the underlying principles, but I'm not used to the generator-as-coroutines concept which is very new to me.</p>
  
  <p>/EDIT: here is a sample of my code, which I must emphasize again is not working:</p>
  
  <pre><code>@coroutine
  def someroutine():
      with open('test.txt', 'a') as f:
          f.write('A')
      while True:
          pass
      yield 0
  
  @coroutine
  def spawnCoroutine():
      result = yield someroutine()
  
      yield result
  
  routine = spawnCoroutine()
  print 'I am working in parallel!'
  
  # Save 'A' in the file test.txt, but does not output 'I am working in parallel!'
  </code></pre>
  
  <p>Note: @coroutine is a decorator from <a href="http://www.dabeaz.com/coroutines/coroutine.py" rel="nofollow">coroutine.py</a> provided by David Beazley</p>
  
  <p><strong>/FINAL EDIT AND SOLUTION RECAP</strong></p>
  
  <p>Ok my question was closed because it was seemingly ambiguous, which as a matter of fact <em>is</em> the very purpose of my question: to clarify the usage of Coroutines over Threading and Multiprocessing.</p>
  
  <p>Luckily, a nice answer was submitted before the dreadly sanction occurred!</p>
  
  <p>To emphasize the answer to the above question: no, Python's coroutines (nor bluelet/greenlet) can't be used to run an independent, potentially infinite CPU-bound task, because <strong>there is no parallelism with coroutines</strong>.</p>
  
  <p>This is what confused me the most. Indeed, <a href="http://jessenoller.com/code/pyworks.pdf" rel="nofollow">parallelism is a subset of concurrency</a>, and thus it is rather confusing that the current implementation of coroutines in Python allow for concurrent tasks, but not for parallel tasks! This behaviour is to be clearly differentiated with the Tasks concept of concurrent programming languages such as Ada.</p>
  
  <p>Also, Python's Threads are similar to coroutines in the fact that they generally switch context when waiting for I/O, and thus are also not a good candidate for independent CPU-bound tasks (see David Beazley's <em>Understanding the GIL</em>).</p>
  
  <p>The solution I'm currently using is to spawn subprocesses with the <code>multiprocessing</code> module. Spawning background processes is heavy, but it's better than running nothing at all. This also has the advantage to allow for distributing computation.</p>
  
  <p>As an alternative, on Google App Engine, there are the <a href="http://code.google.com/appengine/articles/deferred.html" rel="nofollow">deferred module</a> and the <a href="https://developers.google.com/appengine/docs/python/backends/overview#background_threads" rel="nofollow">background_thread module</a> which can offer interesting alternatives to multiprocessing (for example by using some of the libraries that implement the Google App Engine API like <a href="http://code.google.com/p/typhoonae/" rel="nofollow">typhoonae</a>, although I'm not sure they have yet implemented these modules).</p>