views:

448

answers:

2

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

+2  A: 

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?

Anurag Uniyal
Thank you for this - I didn't specifically need a *global* variable just one accessible at class level. I have now modified my code so the globalList[] is defined at class level. I was originally using a global variable because I couldn't get the class level variable to work. I get the same caching with the class level variable though, is there a way to force a reload?
also - the docs say "Global objects live inside mod_python for the life of the apache process". I would have thought that would still reset when the web serivce is restarted though.
yes if you restart apache this should not happen, so are you sure once you restart apache, you see old values?regarding class level are you using instance attribute e.g. in __init__ because class level will still be cached
Anurag Uniyal
you also say "Trac is remembering my login detail" I am not sure abt trac but it be due to cookies on browser side and has nothing to do with server side caching
Anurag Uniyal
+3  A: 

Obligatory:

Switch to wsgi using mod_wsgi.

Don't use mod_python.

There is Help available for configuring mod_wsgi with trac.

nosklo
Thank you for the info