[mod_python] Problem keeping persistent object in memory (global)

Robert Brewer fumanchu at amor.org
Mon Jun 6 12:38:11 EDT 2005


Sébastien Arnaud wrote:
> I am hitting the wall on a very stupid problem that I thought I had  
> working. I can't get an object declared as global to remain 
> in memory...
> Under Apache Pre-fork MPM model, this I understand, but under 
> the MPM  
> Worker model, where I specify to start 10 threads in one process, I  
> kind of don't get it... Maybe some of you will be able to 
> spot what I  
> am doing wrong.
> 
> Basically, what I am trying to do is to keep the object 
> mySitePool in  
> memory since each Site object (heracles.site) has everything needed  
> by a thread to process a request. At the first request the 
> mySitePool  
> object should get initialized, but for any other requests it should  
> be simply the matter of retrieving one Site object via the Queueing  
> mechanism to process one request. Right now the problem is that I am  
> able to see for each request one entry in the apache log file that  
> the mySitePool is being initialized...
> 
...
> 
> Apache2 mod_python (.htaccess):
> -------------------------------
> PythonInterpPerDirective On
> 
> PythonOption "SiteName" "Heracles Test site"
> PythonOption "SiteDescription" "Testing the framework!"
> PythonOption "SiteVirtualPath" "/heracles/"
> PythonOption "SiteViewPath" "/xxx/webapp/views/"
> PythonOption "MySQLhost" "xxx"
> PythonOption "MySQLuid" "arnaudsj"
> PythonOption "MySQLpwd" "xxx"
> PythonOption "MySQLdb" "test"
> 
> SetHandler python-program
> PythonHandler heracles.site::handler
> 
> PythonDebug On
> PythonAutoReload On
> 
> 
> heracles.site.py:
> -----------------
> [...]
> def handler(req):
>      """
>      Standard mod_python handler code for Heracles Web Application  
> Framework
>      The SitePool is initialized based on Apache MPM setting
>      """
>      global mySitePool
>      try:
>          mySite = mySitePool.get()
>      except NameError:
>          req.log_error("Initializing Heracles WAF at %s with 
> %s thread 
> (s)" % (req.document_root(), apache.mpm_query(6)))
>          mySitePool = Pool(Constructor(Site, req), 
> apache.mpm_query(6))
>          mySite = mySitePool.get()
>      return mySite(req)
>      mySitePool.put(mySite)
> [...]

This may or may not fix your problem, but I highly recommend you move the construction of mySitePool out of the handler function, and use a PythonImport directive instead:

In your htaccess file, add:

PythonImport heracles.site interpretername

...and change your site module to read:

[...]
mySitePool = Pool(Constructor(Site), apache.mpm_query(6))

def handler(req):
     """
     Standard mod_python handler code for Heracles Web Application  
Framework
     The SitePool is initialized based on Apache MPM setting
     """
     mySite = mySitePool.get()
     return mySite(req)
     mySitePool.put(mySite)
[...]

...although that will take some rework, since you were passing the request object to Constructor...?!? Anyway, that should make debugging a lot easier.


Robert Brewer
System Architect
Amor Ministries
fumanchu at amor.org



More information about the Mod_python mailing list