views:

465

answers:

1

I am trying to profile twisted python code with Heapy. For example (pseudo code):

from twisted.web import resource, server
from twisted.internet import reactor
from guppy import hpy

class RootResource(resource.Resource):
    render_GET(self, path, request):
        return "Hello World"

if __name__ == '__main__':
    h = hpy()
    port = 8080
    site = server.Site(RootResource(mq))
    reactor.listenTCP(port, site)
    reactor.run()

What do I need to do to view Heapy profile results in the profile browser?

+4  A: 

After looking over the guppy website and not finding any information about how to launch the profile browser there, I started looking around the guppy source and eventually found guppy/heapy/Prof.py, at the end of which I saw a docstring containing this line:

[0] heapy_Use.html#heapykinds.Use.pb

Then, remembering that I had see some documentation giving the return type of guppy.hpy as Use, I checked to see if guppy.hpy().pb() would do anything. And, indeed, it does. So that appears to be how the profiler browser is launched. I'm not sure if this is what you were asking, but I needed to figure it out before I could answer the other possible part of your question. :)

It seems the simplest way to make this information available would be to make a resource in your web server that invokes Use.pb as part of its rendering process. There are other approaches, such as embedding a manhole in your application, or using a signal handler to trigger it, but I like the resource idea. So, for example:

class ProfileBrowser(Resource):
    def render_GET(self, request):
        h.pb()
        return "You saw it, right?"

...
root = RootResource(mq)
root.putChild("profile-browser", ProfileBrowser())
...

Then you can visit /profile-browser whenever you want to look at the profile browser. The "pb" call blocks until the profile browser is exited (note, just closing the window with the wm destroy button doesn't seem to cause it to return - only the exit menu item seems to) so your server is hung until you dismiss the window, but for debugging purposes that seems like it may be fine.

Jean-Paul Calderone