views:

165

answers:

3

At one point, my Django app needs to load one of its own pages to render another page. I'm trying to use urllib2 (working with Python 2.6) to load the page, but it appears that the newer request is blocked until the former completes.

Is this a problem with Django using only one thread in debug mode? (I'm running it simply with the default python manage.py runserver). Is there a simple way of asking it to multithread?

+2  A: 

don't try to use http to itself. even if you get it running (might work on real servers, not in the test one); it would be terribly inefficient. views are just Python functions, call them!

of course, much better is to have a sane model and a separate template fragment that you can use on both views.

Javier
+1  A: 

Why not just use render_to_string?

Swaroop C H
+5  A: 

There really isn't a way to get the dev server to multi-thread. There's been an open ticket about this in the django project for a couple years, and a patch (which you could try implementing to see if it works for you ;-) ). The ticket keeps getting closed or deferred to "design decision needed" because the project does not want folks to ever deploy using the management server... if it is multi-threaded, they just might "in emergencies". The management server is not efficient, nor tightened for security, so any chance that someone might run a production environment with it is quite dangerous... having django installs the world over develop a reputation for poor security wouldn't help :-)

My personal experience has been to run a local Apache instance wired-up like my production environment to test AJAX or other scenarios where you might get concurrent requests.

In your case, since you're just looking for the results of a request and you have access to the server code, I'd recommend a bit of refactoring so that you can get the results of that page's output without having to call that page via HTTP. Write a function that returns the actual result, and have your normal HTTP view simply call that function. This way any other app code can call that function too, without a server round-trip.

Jarret Hardie
Good answer, except that (since 1.0) Django itself is indeed thread-safe. If you've experienced otherwise, the problems are almost certainly in your code (it's very easy to write thread-unsafe code). Since you've marked this "community wiki", I'm going to go ahead and edit to remove that inaccuracy.
Carl Meyer
Thanks Carl... indeed, I have not tried threading in Django since 0.96, so glad to hear about 1.0.
Jarret Hardie
Threading issues still keep arising in Django, even after 1.0. Best to keep an eye on the page http://code.djangoproject.com/wiki/DjangoSpecifications/Core/Threading, for current status of things. Do note though that when that page says something is fixed, it may only be fixed in trunk and not actually released at that point in time. So you really need to check any tickets which mention multithreading.
Graham Dumpleton