Amit Ramon wrote: > Hello, > > I'm developing a site that need to display ads. The ad to display on a page > is selected by a function based on the value of a global state - an > application state that need to be maintained across requests and different > users. In other words, the state is a "website singleton". > > This state is read and changed on every request. One trivial way to achive > this is to store the state in the database, but I'm afraid that changing the > value in the database with every request might create a performance > bottleneck (I'm using MySql, don't know if it matters). > > I thought about having a global (plain python) variable to store the state. > However, there are some points to think about, especially since I'm not sure > how FastCGI works: > 1. Is there single process that handles all the requests? (so it can keep > state) > 2. If so, are different requests handles by separate threads? If this is > true, then I probably need to make this state variable thread-safe. > 3. How long does the FastCGI process (or the relevant application process) > lives? I don't mind reseting the state from time to time, but not too often. > > The ad selection mechanism is somewhat probabilstic - ads should be displayed > more or less randomly (with some uniform or weighted distribution), so > another option I'm thinking of is using python's random() function to select > an ad. That way I won't have to maintian state explicitly, although a state > would be maintained by python for the random number generator. This also > raises some questions: again, to make this work well there need to be a > single server process, the random number generator (or its state) need to be > global. > > I hope this is not too long, and clear enough. I would appreciate any ideas. > > Thanks, > > Amit Ramon ...... I think that unfortunately the top level django process spawns new processes (or threads) to handle concurrent requests. It all depends on the fcgi mechanism. I suspect the fcgi protocol assumes stateless behaviour like much else of the HTTP world so there's probably no obvious way to share state. Certainly the fork model essentially duplicates the parent and child before the child continues so there must be some way for the parent to communicate with the child, but I don't think there's any obvious way for the child to communicate back.
In the separated case you really want the distribution function to satisfy a certain property eg additivity so having two independent generators and summing the results should look like the same distribution as one or three etc etc except perhaps for scale. Unfortunately I don't think that's true in general, it's certainly true for gaussians. If you really want to to this with a single generator then why not pass the add selection problem off to a service on your server ie put the whole problem into a singleton server with a fast response using say XMLRPC if all this has to do is compute a single draw from your function it should be fairly fast. -- Robin Becker --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---

