8

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?

trincot
  • 317,000
  • 35
  • 244
  • 286
roder
  • 566
  • 6
  • 13

1 Answers1

6

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
  • 47,755
  • 6
  • 94
  • 122
  • where did you instantiate h? Or more specifically where do I instantiate h if I am running it using twistd? – Anuvrat Parashar Sep 06 '13 at 07:20
  • I just re-used the `h` variable from the question - where all the code is in the same module and `h` is a global instantiated before the reactor is started. This certainly isn't ideal practice for real world code. You may want to do something like instantiating `hpy` in the `__init__` of one of the important classes in your application and saving it as an attribute. Another idea is to instantiate it in a tac file and attach it to the application object. – Jean-Paul Calderone Sep 06 '13 at 11:02