Goro
Goro

Reputation: 10249

Multi-threading under Apache resulting in duplicate threads

I am running a python thread pool under Apache2 to handle incoming special HTTP requests.

The idea is that I have a single "handler" thread per request source - so if I have three devices (A,B,C) sending me these special requests, each would have its own "handler" thread on the server (1-A, 2-B, 3-C)

I have a thread pool class defined like this:

class threadController(threading.Thread)
   threadPool = []

And when I get a new request, I look through all my running threads, to match a particular one, and pass the request to it.

This seemed to work well enough under Windows.

However, on Linux, it seems that sometimes my threadPool variable returns as empty, and I get an extra thread. So I have a single device (A) sending requests, but end up with two threads (1-A and 2-A).

Here's the strange thing: It is always one extra thread, never more. Regardless whether my device (A) sends 5 requests, or 30.

I am using mod_wsgi (3.3) for django integration.

Note: I realize that this is a somewhat unorthodox way of handling sessions. I am not looking for a way to handle sessions better - I already know there are better ways :)

Upvotes: 0

Views: 865

Answers (1)

Graham Dumpleton
Graham Dumpleton

Reputation: 58523

On Windows there is only one Apache child process handling requests. On non Windows systems, if using embedded mode there can be multiple processes.

Use mod_wsgi daemon and its default of a single process. See:

http://code.google.com/p/modwsgi/wiki/QuickConfigurationGuide#Delegation_To_Daemon_Process

and:

http://code.google.com/p/modwsgi/wiki/ProcessesAndThreading

Upvotes: 1

Related Questions