Windows – Django mutli-threading on apache/mod_wsgi/windows

apache-2.2djangomod-wsgimulti-threadingwindows

I am quite new to the combination of apache and django and quite naive frankly regarding to how multi-threading is handeled. Specifically I run on a windows server, so I know the MPM is only thread-based.

I have an application which serves a website's pages, and another application which performs a cpu intensive background process which happens from time to time and takes as much as 30 seconds or so.
I am worried that I might run into performance issues and have several questions, I hope these will help others as well…

  1. How does apache distribute concurrent requests between threads? and between CPUs? What kind of logic drives this? Does each request spawn a separate thread?
  2. Can I manually specify a certain app to be run on a separate CPU? For example, if I have a machine with 4 CPUs, can I set 1 CPU for website requests and 3 other for running the background process on demand?
  3. How do multiple threads read/write to same database? I don't expect to have collisions since writing will be carried out per-user, but I don't know how this is actually handeled.
  4. What happens if I have more than one such machine, for example running on EC2 and having more than one instance? How are requests distributed?

Thank you

Best Answer

For general reading see:

How does apache distribute concurrent requests between threads?

For a multithreaded configuration there is a pool of pre created threads. When a new request arrives, that gets handed off to one of the available threads in the pool to process. When done the thread goes back into the pool.

How does apache distribute concurrent requests between CPUs?

It doesn't, where a thread runs at any specific point in time is determined by the operating system. Threads are not bound to a specific processor by Apache, so execution can move around.

Does each request spawn a separate thread?

For web requests handled by Apache the answer is no.

Can I manually specify a certain app to be run on a separate CPU?

There are ways that you can set processor affinity to bind processes to processors, but how you do that is system dependent. Apache itself doesn't as far as I know provide any configuration mechanism for doing that to itself.

How do multiple threads read/write to same database?

Carefully. Usually the database server process worries about all that and so you don't have to worry. In a client, you do need to make sure though that each thread is using its own database connection, by creating one for each request, or you have a pool of database connections and threads grab a connection from the pool when required and return it when done.

Note that this presumes a database where access is mediated via a server process. If you are using a file system based database, then the database client library should ensure multithread access is safe.

What happens if I have more than one such machine, for example running on EC2 and having more than one instance? How are requests distributed?

You would need some sort of front end load balancer to distribute requests. That may be a proper load balancer, or you might use a nginx front end as proxy.