views:

213

answers:

2

Hi,

I have a django site that demonstrates the usage of a tool. One of my views takes a file as input and runs some fairly heavy computation trough an external python script and returns some output to the user. The tool runs fast enough to return the output in the same request though. I would however want to limit how many concurrent requests to this URL/view to keep the server from getting congested.

Any tips on how i would go about doing this? The page in itself is very simple and the usage will be low.

A: 

This is really a question about mod_wsgi, assuming you are using that to serve your application from Apache (and if you aren't, you really should be).

The creator of mod_wsgi, Graham Dumpleton, sometimes hangs around these parts, but while waiting for him to turn up, you might look at the superb documentation. Looks like one or other of the arguments to the WSGIDaemonProcess directive might be what you're looking for.

Daniel Roseman
Dob me in hey. :-(
Graham Dumpleton
Sorry... in my defence, I did call your documentation 'superb'.
Daniel Roseman
+1  A: 

Presuming you are using mod_wsgi, you could segment your WSGI application over multiple mod_wsgi daemon process groups with the specific URLs you want to restrict delegated to a mod_wsgi daemon process group of their own with a lesser number of processes/threads.

WSGIDaemonProcess myapp processes=2 threads=10
WSGIDaemonProcess myapp-restricted threads=2

WSGIScriptAlias / /some/path/app.wsgi

WSGIProcessGroup myapp

<Location /some/specific/url>
WSGIProcessGroup myapp-restricted
</Location>

Note that the requests queued up to get access to those 2 threads in the restricted process will still consume a worker thread in main Apache server child processes. Thus, you want to make sure you use worker MPM (so no mod_php) and configure the MPM with sufficient spare threads to handle those backed up requests as well as normal traffic.

Graham Dumpleton
Thanks for the answers. I was trying to do something similar as you Graham but i kept getting a 513 error until i realized it was a permssion error but your documentation helped me fix that. So you are saying that WSGIDaemonProcess shouldnt be run with prefork mpm at all?On a different thing. Would it be possible to drop requests that cannot be served directly and redirect the user to a error page instead?
Johan
You can still use prefork MPM. You just need to realise that each parked request which gets queued up will use up a whole Apache server child process rather than just a single thread if worker MPM were used. As such, prefork MPM has a higher memory overhead than if worker MPM used when using mod_wsgi daemon mode. If you have heaps of memory no problem, of if you are using mod_php, you have no choice. And no, no way to drop a request that couldn't be served immediately because of the restricted process being busy.
Graham Dumpleton
Okey thanks, ill have a look into worker MPM.
Johan