mod_python caching of variables

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


Asked by: Elian624 | Posted: 06-12-2021






Answer 1

Obligatory:

Switch to wsgi using mod_wsgi.

Don't use mod_python.

There is Help available for configuring mod_wsgi with trac.

Answered by: Brad177 | Posted: 07-01-2022



Answer 2

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?

Answered by: Thomas799 | Posted: 07-01-2022



Similar questions

python - Mod_python not getting GET variables

I have standard 13.10 Ubuntu. Running Apache 2 and mod_python. Both installed using apt-get. I'm trying to pass a GET variable from the client to the server, execute python script and then return a result. Here is my ApacheConfig (Default plus code below): ScriptAlias /tm/ "/home/tm/" <Directory /home/tm/ > Require all granted Options Indexes FollowSymLinks MultiViews ...


python - Why are my mod_python global variables resetting?

I have a server using apache2 with mod_python that seems to reset global variables after some short period of time. Until this happens, all the features of the server and global variables are handled exactly as I would expect. To rule out the possibility of some runtime error (although I do have debugging on and don't see any errors), I made this simplified script to demonstrate how a global variable is being reset...


python - Django, mod_python, apache and wacky sessions

I am running a Django through mod_python on Apache on a linux box. I have a custom authentication backend, and middleware that requires authentication for all pages, except static content. My problem is that after I log in, I will still randomly get the log in screen now and again. It seems to me that each apache process has it's own python process, which in turn has it's own internals. So as long as I get served b...


python - Failed to get separate instances of a class under mod_python

I'm trying to run some python code under Apache 2.2 / mod_python 3.2.8. Eventually the code does os.fork() and spawns 2 separate long-run processes. Each of those processes has to create a separate instance of a class in order to avoid any possible collision in the parallel flow. class Foo(object): pass kidprocs = [] for kid in ('kid1', 'kid2'): pid = os.fork() if pid: # parent kidprocs.a...


python - Running Django with FastCGI or with mod_python

which would you recommend? which is faster, reliable? apache mod_python or nginx/lighttpd FastCGI?


python - How to access to the root path in a mod_python directory?

In my Apache webserver I put this: <Directory /var/www/MYDOMAIN.com/htdocs> SetHandler mod_python PythonHandler mod_python.publisher PythonDebug On </Directory> Then I have a handler.py file with an index function. When I go to MYDOMAIN.com/handler.py, I see a web page produced by the index function (just a plain vanilla HTML page). Every other page is of th...


python - How do I modify sys.path from .htaccess to allow mod_python to see Django?

The host I'm considering for hosting a Django site has mod_python installed, but does not have Django. Django's INSTALL file indicates that I can simply copy the django directory to Python's site-packages directory to install Django, so I suspect that it might be possible to configure Python / mod_python to look for it elsewhere (namely my user space) by modifying sys.path, but I don't know how to change it from .htaccess ...


php - mod_php vs mod_python

Why mod_python is oop but mod_php is not ? Example :We go to www.example.com/dir1/dir2 if you use mod_python apache opens www/dir1.py and calls dir2 method but if you use php module apache opens www/dir1/dir2/index.php


python - How do I upload a file with mod_python?

I want to create a simple file upload form and I must be completely incapable. I've read docs and tutorials,but for some reason, I'm not getting the submitted form data. I wrote the smallest amount of code I could to test and it still isn't working. Any ideas what's wrong? def index(): html = ''' <html> <body> <form id="fileUpload" action="./result" method="post"> ...


python - mod_python publisher and pretty URLs

I am new to Python (I am getting out of PHP because of how increasingly broken it is), and I am racing through porting my old code. One thing: I have a file /foo.py with functions index() and bar(), so, with the publisher I can access http://domain/foo/bar and http://domain/foo as the documentatio...


python - Configure Apache to recover from mod_python errors

This question already has an answer here:


django - Different behavior of python logging module when using mod_python

We have a nasty problem where we see that the python logging module is behaving differently when running with mod_python on our servers. When executing the same code in the shell, or in django with the runserver command or with mod_wsgi, the behavior is correct: import logging logger = logging.getLogger('site-errors') logging.debug('logger=%s' % (logger.__dict__)) logging.debug('logger.parent=%s' % (logger....






Still can't find your answer? Check out these communities...



PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python



top