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.
My question is: Is this a limitation of Django's development server or am I doing it wrong?
The server does not completely hangs, as other views can be processed in the meantime.
There are already a few questions on that topic and Google gives a few other threads, but I cannot find a clear answer to my question.
I believe this is not a python issue as this commands terminate immediately:
python -c 'import subprocess; print subprocess.Popen(["/bin/sleep", "10"]).pid'
How to reproduce
Create test project & app:
cd /tmp
django-admin.py startproject django_test
cd django_test
./manage.py startapp subprocess_test
Replace urls.py & subprocess_test/views.py with:
urls.py:
from django.conf.urls.defaults import *
urlpatterns = patterns('',
(r'^hello$', 'subprocess_test.views.hello'),
(r'^start$', 'subprocess_test.views.start'),
)subprocess_test/views.py
from django.http import HttpResponse
import subprocess
def hello(request):
return HttpResponse('Hello world!')def start(request):
subprocess.Popen(["/bin/sleep", "10"])
return HttpResponse('start done')
Test it:
./manage.py runserver 0.0.0.0:8000
Go to http://127.0.0.1:8000/hello and http://127.0.0.1:8000/start
Test result
"start" takes 10s to load and "hello" can be loaded during that time. For example, I get such a log:
[01/Feb/2011 07:20:57] "GET /hello HTTP/1.1" 200 12
[01/Feb/2011 07:21:01] "GET /start HTTP/1.1" 200 10
[01/Feb/2011 07:21:01] "GET /hello HTTP/1.1" 200 12
[01/Feb/2011 07:21:02] "GET /hello HTTP/1.1" 200 12
Using wget:
wget http://127.0.0.1:8000/start
--2011-02-01 14:31:11-- http://127.0.0.1:8000/start
Connecting to 127.0.0.1:8000... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/html]
Saving to: `start'[ <=> ] 10 --.-K/s in 9,5s
2011-02-01 14:31:21 (1,05 B/s) - « start » saved [10]