On Sat, Aug 15, 2009 at 7:03 AM, pydon<[email protected]> wrote: > > Hi all, > > To start off I would like to use a global variable to store a simple > counting object (integers). The problem is keeping track of it because > when the server is shutdown it will disappear. I would like to on > server shutdown write this data to a database and then on server > startup read it back to the global variable. > > Being a small counter it won't really grow much if at all but will get > a lot of reads and writes upon it. Due to the IO requirement on it > having a constant database update each time it changes would be slow. > > So my main question is. Can paster run a python script(py file with > class) on server startup (in production mode) and then on server > shutdown run the script again. If so, how? When I shutdown the server > with Ctrl-C it simply justs stops the threads.
Startup is easy: do it in environment.py. For shutdown, you could register an atexit function (see atexit module), but it doesn't work if Python is killed by a signal, which is probably the way you're stopping the application. Perhaps a signal handler (signal module). You may want to save the counter in a file rather than in a database, so that SQLAlchemy doesn't have to be fully initialized when reading/writing it. Memcached might also help. It would still be in memory, but at least in a different process that wouldn't go down when the application does. I'm not sure if memcached can be backed by a persistent store, but that would be ideal. Alternatively, if the number doesn't have to be exact, you could update it after every 100 increments. You could also combine this with a shutdown procedure so that the count won't be completely lost if the program crashes. -- Mike Orr <[email protected]> --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "pylons-discuss" 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/pylons-discuss?hl=en -~----------~----~----~----~------~----~------~--~---
