Huzaifa Tapal
huzaifa at hostway.com
Mon Jun 6 13:02:48 EDT 2005
One option that I use is to create a singleton in the module that I want to be global at the first import of that module. So lets take your mySitePool module for example: class mySitePool: def __init__(self): ' suite .... global mysitepool mysitepool = mySitePool() Now, in your mod_python request handler, when you add the import of mySitePool, the global singleton object of mySitePool will be created. Since, the module is imported, in mod_python, that module will be cached so it would not get imported again so to recreate the singleton. So your handler would look like this: import mySitePool def handler(req): mySite = mySitePool.mysitepool.get() return mySite(req) mySitePool.mysitepool.put(mySite) Let me know if this helps. Hozi Sébastien Arnaud wrote: > Hi, > > I have attempted to write my own little framework ("heracles" I named > it;) to develop applications faster using mod_python and it is coming > along great. Mainly I am using the framework to keep handy a bunch of > little utils functions/objects I have written along the years while > working with mod_python, such as a pool of connections for DB > connections, XML/XSLT rendering, Cheetah template rendering, etc... > > 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... > > Thank you in advance for any pointers or solutions you may have! > > Apache2 config (MPM worker): > --------------------------- > <IfModule worker.c> > StartServers 1 > MaxClients 10 > MinSpareThreads 10 > MaxSpareThreads 0 > ThreadsPerChild 10 > MaxRequestsPerChild 0 > </IfModule> > > > 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) > [...] > > > Cheers, > > Sébastien > > > > > > _______________________________________________ > Mod_python mailing list > Mod_python at modpython.org > http://mailman.modpython.org/mailman/listinfo/mod_python > >
|