Persisting data in a module for hit counter
I have a dictionary that I want to share between request, but I do not want it to be persistent in the zodb. Rather I want to pickle it and store it on the filesystem. Getting the pickled data is easy enough, as I only need to read the pickled file at the module level. The problem is when zope is stopped, or a module is reloaded, then it looses all data. Is there any way to know when a module goes out of scope, so that I can get a hook to save/pickle the dictionary? Or at least a way to know when zope is shut down? My alternative option is to set a counter that saves the data after n number of write to the dictionary. This can work, as it is not critical data anyway. -------------------- It's a simple hit counter/stat module, that stores counts as: counters = { urlpath:count, } It has the advantage over FScounter in that it only needs one instance to work for all pages in a site. Ie. if you put this code into the footer of a Plone site, you will get a counter on all pages.: This page has been visited: <span tal:content="python:here.mxm_counter.count(here)"/> times. Also you can have a nice statistics page with a list of visited pages. It works nicely allready, but only between restarts :-) regards Max M
Max M wrote at 2004-2-25 15:43 +0100:
... The problem is when zope is stopped, or a module is reloaded, then it looses all data.
Is there any way to know when a module goes out of scope, so that I can get a hook to save/pickle the dictionary? Or at least a way to know when zope is shut down?
There is not reliable way, as Zope may die unexpectedly (e.g. caused by a signal). "refresh" lets you control that some modules are not reloaded during a refresh. There is no hook to intercept module deletion. There is a hook to get control at program exit. It is called "atexit" (or similar). As modules usually do not disappear by themselves, this may be enough. As said, it is not reliable in case of abnormal terminations. -- Dieter
participants (2)
-
Dieter Maurer -
Max M