views:

52

answers:

2

I have a simple django page that has a counter on it. I use Apache2 with mod_wsgi to serve it.

First, when I enter this page, the counter shows 0, as it should. The second time when I enter the page the counter shows 1 - again, it is the right behavior. The problem begins now, cause when I enter this page the third time, I get 0 again.

When I refresh it goes between 0, and 1, clearly using some cache or so. If I wait for some time and then try again, it will show 2, and 3, but will be stuck with those values, till this cache or whatever it is will be flushed, and then the counter continues.

Does somebody knows how I can get it work right (the real scenario deals with getting data from the DB, but the problems with this strange cache are the same).

BTW, I don't have any caching engine set in my django settings.

A: 

You are running multiple instances of your Django. Apache connects randomly to one of them.

For testing purposes, try this in your apache.conf:

WSGIDaemonProcess mysite processes=1 maximum-requests=500 threads=1

(You may want more processes in production use.)

See full documentation of options.

jholster
Thanks! This simple answer didn't occur to me for some reason.Do you know how do I change the number of those instances, and what are the consequences of reducing the number of instances to 1?
Adam
Number of instances == processes * threads. If only one instance is running, only one client can be served at once, making your application slow for users, assuming there are multiple simultaneous users.
jholster
+2  A: 

Number of instances != processes * threads as suggested by other poster. Number of instances == processes only.

Read:

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

Likely you are running embedded mode. You should instead use daemon mode.

Don't set 'processes=1' though as that is the default anyway and using the option has other side effects you may not want.

The default number of threads for daemon mode is 15 which is fine so long as your application is thread safe. Specifically, access to your global counter should be thread safe.

Also read:

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

which has an example of how to set up daemon mode. You need to use both WSGIDaemonProcess and WSGIProcessGroup directives, having just WSGIDaemonProcess is not enough.

Graham Dumpleton
Thanks for info. Isn't it still correct to say _number of simultaneous served clients_ = processes * threads?
jholster
The theoretical maximum on number of concurrent requests is processes * threads, but all threads in a process normally run in same interpreter instance for a specific application. Read 'http://blog.dscpl.com.au/2009/03/python-interpreter-is-not-created-for.html', I think I talk a bit about this sort of stuff in there.
Graham Dumpleton