[mod_python] handlers and globals

Neil Gower ngower at dkp.com
Fri Nov 11 12:15:44 EST 2005


Thanks Graham!  It works like a charm.  I had totally forgotten about the 
Profile class... the downside to convenience methods I guess.  :-)

I'll see how it goes re: meaningful timing data... I expect that for slow 
enough pages the profiler will yield some useable stats, which is really what 
I'm worried about.  I think I've got some PSP pages doing some pretty heavy 
processing, hopefully now I'll have the stats to prove it.

Cheers,


Neil


Graham Dumpleton wrote:
> Use this:
> 
>      # but this doesn't...
>      #rc = profile.run( 'processPsp()', 'ProfilerTest.psp.prof' )
>      #return rc
>      
>      pobject = profile.Profile()
>      presult = pobject.runctx('processPsp()',globals(),locals())
>      presult.dump_stats("/tmp/ProfilerTest.psp.prof")
> 
> The default run() method uses globals from __main__ module and
> thus why it can't find symbol.
> 
> Also need to specify absolute path for where stats are being saved
> as no gaurantee as to what current working directory is when run
> in the context of mod_python.
> 
> To print out stats do the following, although a single call doesn't
> give you enough data for meaningful time values unless there is
> something I missed.
> 
> /tmp [504]$ python
> Python 2.3 (#1, Sep 13 2003, 00:49:11) 
> [GCC 3.3 20030304 (Apple Computer, Inc. build 1495)] on darwin
> Type "help", "copyright", "credits" or "license" for more information.
> 
>>>>import pstats
>>>>stats = pstats.Stats("/tmp/ProfilerTest.psp.prof")
>>>>stats.print_stats()
> 
> Fri Nov 11 14:14:46 2005    /tmp/stats.dat
> 
>          19 function calls in 0.000 CPU seconds
> 
>    Random listing order was used
> 
>    ncalls  tottime  percall  cumtime  percall filename:lineno(function)
>         1    0.000    0.000    0.000    0.000 /System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/site-packages/mod_python/psp.py:123(cache_get)
>         1    0.000    0.000    0.000    0.000 /System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/site-packages/mod_python/psp.py:60(__init__)
>         1    0.000    0.000    0.000    0.000 /System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/posixpath.py:74(split)
>         1    0.000    0.000    0.000    0.000 /System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/site-packages/mod_python/psp.py:91(__init__)
>         1    0.000    0.000    0.000    0.000 /System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/posixpath.py:47(isabs)
>         1    0.000    0.000    0.000    0.000 /System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/posixpath.py:197(isfile)
>         1    0.000    0.000    0.000    0.000 /System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/posixpath.py:144(getmtime)
>         1    0.000    0.000    0.000    0.000 /System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/site-packages/mod_python/psp.py:139(cache_store)
>         1    0.000    0.000    0.000    0.000 /System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/site-packages/mod_python/psp.py:403(store)
>         1    0.000    0.000    0.000    0.000 /System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/posixpath.py:56(join)
>         1    0.000    0.000    0.000    0.000 /System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/stat.py:29(S_IFMT)
>         1    0.000    0.000    0.000    0.000 /Users/grahamd/Sites/profiler/ProfilerTest.psp:1(?)
>         1    0.000    0.000    0.000    0.000 /System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/site-packages/mod_python/psp.py:186(run)
>         1    0.000    0.000    0.000    0.000 /System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/stat.py:54(S_ISREG)
>         1    0.000    0.000    0.000    0.000 /System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/site-packages/mod_python/psp.py:408(get)
>         1    0.000    0.000    0.000    0.000 /Users/grahamd/Sites/profiler/profiler-handler.py:11(processPsp)
>         0    0.000             0.000          profile:0(profiler)
>         1    0.000    0.000    0.000    0.000 profile:0(processPsp())
>         1    0.000    0.000    0.000    0.000 /System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/site-packages/mod_python/psp.py:159(load_from_file)
>         1    0.000    0.000    0.000    0.000 <string>:1(?)
> 
> 
> <pstats.Stats instance at 0x41d78>
> 
> 
> Graham
> 
> 
> Neil Gower wrote ..
> 
>>Hello,
>>
>>I'm having some trouble trying to run my PSP pages through the standard
>>Python profiler.  The problem appears to be that the python globals don't
>>behave as the profiler expects when it is run through mod_python.
>>
>>The test handler (below) works okay with the profiler when invoked from
>>a 
>>regular command line Python interpreter, but it raises a "NameError: name
>>'processPsp' is not defined" exception when run through mod_python.  I'm
>>taking that to mean that the profiler can't find the processPsp() method
>>to 
>>invoke.
>>
>>Can anyone offer any suggestions?  I'm stumped at this point.
>>
>>Thanks!
>>
>>
>>Neil.
>>
>>
>>Details of my setup:
>>=============================================================================
>>Mod_python error: "PythonHandler profiler-handler"
>>
>>Traceback (most recent call last):
>>
>>   File "/usr/lib/python2.3/site-packages/mod_python/apache.py", line 299,
>>in 
>>HandlerDispatch
>>     result = object(req)
>>
>>   File "/var/profiler-test/profiler-handler.py", line 36, in handler
>>     rc = profile.run( 'processPsp()', 'ProfilerTest.psp.prof' )
>>
>>   File "/usr/lib/python2.3/profile.py", line 71, in run
>>     prof = prof.run(statement)
>>
>>   File "/usr/lib/python2.3/profile.py", line 403, in run
>>     return self.runctx(cmd, dict, dict)
>>
>>   File "/usr/lib/python2.3/profile.py", line 409, in runctx
>>     exec cmd in globals, locals
>>
>>   File "", line 1, in ?
>>
>>NameError: name 'processPsp' is not defined
>>=============================================================================
>>Apache:
>>
>><Directory "/var/profiler-test">
>>     Options Indexes MultiViews
>>     AllowOverride None
>>     Order allow,deny
>>     Allow from all
>>
>>     AddHandler mod_python .psp
>>     PythonHandler profiler-handler
>>     PythonDebug On
>></Directory>
>>
>>=============================================================================
>>/var/profiler-test/ProfilerTest.psp:
>>
>><p>This is a test!
>>
>><%
>>req.write( "<p>So is this!" )
>>%>
>>
>>=============================================================================
>>/var/profiler-test/profiler-handler.py:
>>
>># we have to wrap these imports in a try block to be able to run the
>># code outside of mod_python, where they will fail.
>>try:
>>     from mod_python import apache, psp
>>except:
>>     pass
>>
>>import profile
>>
>>
>>def processPsp():
>>     """
>>     A plain old method that uses global variables for the PSP
>>     parameters, which makes it easy to use with the profiler.
>>     """
>>     theRequest.content_type = "text/html"
>>     template = psp.PSP( theRequest, theTemplateFile )
>>     template.run()
>>     return apache.OK
>># end processPsp()
>>
>>
>>def handler( req ):
>>     """
>>     The mod_python hook for handling a page request.
>>     """
>>     global theRequest
>>     global theTemplateFile
>>     theRequest = req
>>     theTemplateFile = 'ProfilerTest.psp'
>>
>>     # This works...
>>     #rc = processPsp()
>>
>>     # but this doesn't...
>>     rc = profile.run( 'processPsp()', 'ProfilerTest.psp.prof' )
>>
>>     return rc
>># end handler()
>>
>>
>>if __name__ == '__main__':
>>     try:
>>         handler( None )
>>     except AttributeError, e:
>>         # if we actually make it into processPsp(), the null request
>>         # object will cause an AttributeError.
>>         print "A not unexpected error: %s" % str(e)
>># end main.
>>=============================================================================
>>_______________________________________________
>>Mod_python mailing list
>>Mod_python at modpython.org
>>http://mailman.modpython.org/mailman/listinfo/mod_python


More information about the Mod_python mailing list