title: subprocess blocks Django view
body: <p>I have a problem calling subprocess.Popen from a view:
  The view that calls subprocess.Popen is not displayed until the subprocess finishes.
  The server send "200 OK" immediately, but not the content of the page.</p>
  
  <p>My question is: Is this a limitation of Django's development server or am I doing it wrong?</p>
  
  <p>The server does not completely hangs, as other views can be processed in the meantime.</p>
  
  <p>There are already <a href="https://stackoverflow.com/search?q=Django+subprocess">a few questions on that topic</a> and Google gives <a href="http://www.google.com/#q=django+runserver+subprocess" rel="nofollow noreferrer">a few other threads</a>, but I cannot find a clear answer to my question.</p>
  
  <p>I believe this is not a python issue as this commands terminate immediately:</p>
  
  <blockquote>
    <p>python -c 'import subprocess; print subprocess.Popen(["/bin/sleep", "10"]).pid'</p>
  </blockquote>
  
  <h2>How to reproduce</h2>
  
  <h3>Create test project &amp; app:</h3>
  
  <blockquote>
    <p>cd /tmp<br>
    django-admin.py startproject django_test<br>
    cd django_test<br>
    ./manage.py startapp subprocess_test</p>
  </blockquote>
  
  <h3>Replace urls.py &amp; subprocess_test/views.py with:</h3>
  
  <ul>
  <li><p>urls.py:</p>
  
  <p>from django.conf.urls.defaults import *  </p>
  
  <p>urlpatterns = patterns('',<br>
  &nbsp;    (r'^hello$', 'subprocess_test.views.hello'),<br>
  &nbsp;    (r'^start$', 'subprocess_test.views.start'),<br>
  )  </p></li>
  <li><p>subprocess_test/views.py</p>
  
  <p>from django.http import HttpResponse  </p>
  
  <p>import subprocess  </p>
  
  <p>def hello(request):<br>
  &nbsp;    return HttpResponse('Hello world!')  </p>
  
  <p>def start(request):<br>
  &nbsp;    subprocess.Popen(["/bin/sleep", "10"])<br>
  &nbsp;    return HttpResponse('start done')</p></li>
  </ul>
  
  <h3>Test it:</h3>
  
  <blockquote>
    <p>./manage.py runserver 0.0.0.0:8000  </p>
  </blockquote>
  
  <p>Go to <a href="http://127.0.0.1:8000/hello" rel="nofollow noreferrer">http://127.0.0.1:8000/hello</a> and <a href="http://127.0.0.1:8000/start" rel="nofollow noreferrer">http://127.0.0.1:8000/start</a></p>
  
  <h2>Test result</h2>
  
  <p>"start" takes 10s to load and "hello" can be loaded during that time.
  For example, I get such a log:</p>
  
  <blockquote>
    <p>[01/Feb/2011 07:20:57] "GET /hello HTTP/1.1" 200 12<br>
    [01/Feb/2011 07:21:01] "GET /start HTTP/1.1" 200 10<br>
    [01/Feb/2011 07:21:01] "GET /hello HTTP/1.1" 200 12<br>
    [01/Feb/2011 07:21:02] "GET /hello HTTP/1.1" 200 12  </p>
  </blockquote>
  
  <p>Using wget:</p>
  
  <blockquote>
    <p>wget <a href="http://127.0.0.1:8000/start" rel="nofollow noreferrer">http://127.0.0.1:8000/start</a><br>
    --2011-02-01 14:31:11--  <a href="http://127.0.0.1:8000/start" rel="nofollow noreferrer">http://127.0.0.1:8000/start</a><br>
    Connecting to 127.0.0.1:8000... connected.<br>
    HTTP request sent, awaiting response... 200 OK<br>
    Length: unspecified [text/html]<br>
    Saving to: `start'  </p>
  
  <pre><code>[           &lt;=&gt;                           ] 10          --.-K/s   in 9,5s    
  </code></pre>
    
    <p>2011-02-01 14:31:21 (1,05 B/s) - « start » saved [10]</p>
  </blockquote>