Tuesday, February 1, 2011

subprocess blocks view

Hello,

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.

Note: Cross-posted on: http://stackoverflow.com/questions/4863405/subprocess-blocks-django-view

## 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
Notice that "start" takes 10s to load and that you can load "hello"
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]

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment