Question

I'm using mod_python to run Trac in Apache. I'm developing a plugin and am not sure how global variables are stored/cached.

I am new to python and have googled the subject and found that mod_python caches python modules (I think). However, I would expect that cache to be reset when the web service is restarted, but it doesn't appear to be. I'm saying this becasue I have a global variable that is a list, I test the list to see if a value exists and if it doesn't then I add it. The first time I ran this, it added three entries to the list. Subsequently, the list has three entries from the start.

For example:

globalList = []

class globalTest:

  def addToTheList(itemToAdd):
      print(len(globalTest))
      if itemToAdd not in globalList:
          globalList.append(itemToAdd)

  def doSomething():
    addToTheList("I am new entry one")
    addToTheList("I am new entry two")
    addToTheList("I am new entry three")

The code above is just an example of what I'm doing, not the actual code ;-). But essentially the doSomething() method is called by Trac. The first time it ran, it added all three entries. Now - even after restarting the web server the len(globalList) command is always 3.

I suspect the answer may be that my session (and therefore the global variable) is being cached because Trac is remembering my login details when I refresh the page in Trac after the web server restart. If that's the case - how do I force the cache to be cleared. Note that I don't want to reset the globalList variable manually i.e. globalList.length = 0

Can anyone offer any insight as to what is happening? Thank you

Was it helpful?

Solution

read the mod-python faq it says

Global objects live inside mod_python for the life of the apache process, which in general is much longer than the life of a single request. This means if you expect a global variable to be initialised every time you will be surprised....

go to link http://www.modpython.org/FAQ/faqw.py?req=show&file=faq03.005.htp

so question is why you want to use global variable?

OTHER TIPS

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top