mod_wsgi and number of processes/threads

2,760 views
Skip to first unread message

Rob Hudson

unread,
Nov 18, 2008, 10:10:44 AM11/18/08
to modwsgi
Is there a general rule of thumb that can guide a person on what
numbers might need to be put for:

WSGIDaemonProcess processes=X threads=Y

I'm curious if knowing what your requests per [time period] is if that
can guide you what to put here. I have a small blog (couple hundred
hits per day?) that I just guess doesn't need much. But I'll be
setting up a larger site (10,000 hits per day?) that I'm not sure of
a good guideline to use.

If there is not guideline, is there a way to know that you need to
adjust these?

Thanks,
Rob

Graham Dumpleton

unread,
Nov 18, 2008, 5:51:39 PM11/18/08
to mod...@googlegroups.com
2008/11/19 Rob Hudson <trebor...@gmail.com>:

Hmmm, even 10000 hits per day is not a large site.

For that would just not bother setting processes or threads and just
let WSGIDaemonProcess default to single process with 15 threads. That
should be more than adequate.

Do note that technically setting 'processes=1' is not the same as not
setting the option. When you use 'processes' option for
WSGIDaemonProcess, even if a single process, the wsgi.multiprocess
flag in WSGI environment is set to True.

Thus, don't say 'processes=1', allow it to be defaulted to 1 instead.
The only time you would want 'processes=1' is if you are running a
cluster of machines and application is spread across them yet only
running single process on each. In this case there are actually
multiple process, just distributed and need way to flag that.

Graham

Rob Hudson

unread,
Nov 18, 2008, 7:44:28 PM11/18/08
to mod...@googlegroups.com
On Tue, Nov 18, 2008 at 2:51 PM, Graham Dumpleton
<graham.d...@gmail.com> wrote:
> Hmmm, even 10000 hits per day is not a large site.

Yeah, that's why I said "larger" to keep it relative. But doing the
math and averaging out hits evenly, that's 1 request every 11 seconds
or so... definitely not large at all.

> For that would just not bother setting processes or threads and just
> let WSGIDaemonProcess default to single process with 15 threads. That
> should be more than adequate.

OK. After doing that on my slicehost account, it looks like it
actually reduced memory compared to what I had previously, so that's a
nice side effect when I'm always looking at how far away the 256M
limit is. Previously I think I was using 2 processes and 1 thread...
no idea where I got that idea from. I think I had it in my mind that
Django shouldn't be run multithreaded, which might have been true at
one point but is no longer true today.

Thanks Graham,
Rob

meppum

unread,
Dec 22, 2008, 1:18:47 AM12/22/08
to modwsgi
What about apache mpm settings? Any examples on what they should be
for say the default WSGIDaemonProcess setting of 15 threads? Thanks.

On Nov 18, 7:44 pm, "Rob Hudson" <treborhud...@gmail.com> wrote:
> On Tue, Nov 18, 2008 at 2:51 PM, Graham Dumpleton
>

Graham Dumpleton

unread,
Dec 22, 2008, 1:37:43 AM12/22/08
to mod...@googlegroups.com
2008/12/22 meppum <mme...@gmail.com>:

>
> What about apache mpm settings? Any examples on what they should be
> for say the default WSGIDaemonProcess setting of 15 threads? Thanks.

The defaults for standard Apache source distribution are:

# worker MPM
# StartServers: initial number of server processes to start
# MaxClients: maximum number of simultaneous client connections
# MinSpareThreads: minimum number of worker threads which are kept spare
# MaxSpareThreads: maximum number of worker threads which are kept spare
# ThreadsPerChild: constant number of worker threads in each server process
# MaxRequestsPerChild: maximum number of requests a server process serves
<IfModule mpm_worker_module>
StartServers 2
MaxClients 150
MinSpareThreads 25
MaxSpareThreads 75
ThreadsPerChild 25
MaxRequestsPerChild 0
</IfModule>

This is a reasonable starting point.

This allows for a lot more concurrent connections, but if you are also
serving static files and default of keepalive being on is still in
use, the maximum of 150 clients is okay, especially since it will only
create more than 2 Apache child worker processes if it really needs
to.

I would only bother starting to fiddle with the defaults when you have
realistic estimates of the sort of load you would need to handle.

Graham

meppum

unread,
Dec 22, 2008, 9:02:13 AM12/22/08
to modwsgi
Okay, lets see if I have this right...

If i have 2 apache servers and each apache server can have 25 threads
that's 50 threads total.

If mod_wsgi is running in daemon mode does that mean that each apache
thread will spawn a mod_wsgi daemon? And if each daemon has 15 threads
does that mean that there can be 50 (apache) x 15 (mod_wsgi) = 750
connections?

On Dec 22, 1:37 am, "Graham Dumpleton" <graham.dumple...@gmail.com>
wrote:
> 2008/12/22 meppum <mmep...@gmail.com>:

Graham Dumpleton

unread,
Dec 22, 2008, 4:37:15 PM12/22/08
to mod...@googlegroups.com
2008/12/23 meppum <mme...@gmail.com>:
>
> Okay, lets see if I have this right...
>
> If i have 2 apache servers and each apache server can have 25 threads
> that's 50 threads total.
>
> If mod_wsgi is running in daemon mode does that mean that each apache
> thread will spawn a mod_wsgi daemon?

No. The mod_wsgi daemon process(es) are spawned by the Apache parent
process. Only the number configured by the 'processes' option to
WSGIDaemonProcess directive will be created. If the 'processes' option
is not defined then only 1 process is created. Note, do not specify
'processes=1' however unless you know why you want to do this. If want
only 1 process let it default to creating 1 instead.

> And if each daemon has 15 threads
> does that mean that there can be 50 (apache) x 15 (mod_wsgi) = 750
> connections?

No. In the case of the WSGI application, the Apache child worker
processes only act as a proxy, forwarding the requests across to the
mod_wsgi daemon mode process(es) for handling.

Thus, with 2 Apache child worker processes, maximum number of
connections is 50 (where each has 25 threads). These Apache child
worker processes accept connections for both static file requests and
dynamic requests which are then proxied across to the mod_wsgi daemon
mode process. With only a single mod_wsgi daemon mode process, the
WSGI application itself will be able to handle 15 concurrent requests.

If there are requests being handled by mod_wsgi daemon mode process,
because the Apache child worker processes is proxying the requests and
responses, a thread is still consumed for the life of the request in
the Apache child worker processes. The 35 (50-15) additional threads
in Apache child worker processes would still be available for handling
static requests, keep alive connections and acting as a buffering
mechanism for pending dynamic requests against WSGI application. The
latter particularly useful for slow clients as apache child worker
processes will not forward request onto mod_wsgi daemon process until
full request information available.

Note that just because mod_wsgi daemon mode process can only handle 15
concurrent requests doesn't mean that it can only handle that many
requests per second. How many requests per second is going to be
dictated by how slow your application is and what contention there is
on shared resources. The latter have an impact on whether operations
need to be serialised.

Overall, just think of mod_wsgi daemon mode as being similar to using
mod_proxy in front of a separate back end web server. In this case
though mod_wsgi has created the daemon processes and is managing them
on your behalf.

Graham

meppum

unread,
Dec 22, 2008, 5:19:44 PM12/22/08
to modwsgi
Good thing I asked. So lets say I'm using nginx for my static file
requests, and that only dynamic requests are fed to apache and that
keepalives are OFF. Would you recommend against setting the apache
process/thread settings equal to that of the mod_wsgi daemon process/
thread settings? Is there a best practice in this case?

Thanks again.

On Dec 22, 4:37 pm, "Graham Dumpleton" <graham.dumple...@gmail.com>
wrote:
> 2008/12/23meppum<mmep...@gmail.com>:
> >> 2008/12/22meppum<mmep...@gmail.com>:

Graham Dumpleton

unread,
Dec 22, 2008, 7:46:04 PM12/22/08
to mod...@googlegroups.com
2008/12/23 meppum <mme...@gmail.com>:
>
> Good thing I asked. So lets say I'm using nginx for my static file
> requests, and that only dynamic requests are fed to apache and that
> keepalives are OFF. Would you recommend against setting the apache
> process/thread settings equal to that of the mod_wsgi daemon process/
> thread settings? Is there a best practice in this case?

Have no strong guidance on this or first hand experience to go by. I
would probably still allow more threads in Apache worker processes.

Although more threads will mean slightly more physical memory usage,
not that substantial. Still depends on what your primary concern is.
That is, whether targeting a specific load capability, or just trying
to minimise memory usage.

Graham
Reply all
Reply to author
Forward
0 new messages