-
-
Save mynameisfiber/5680215 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
""" | |
Monkey Patch for tornado that profiles through async requests | |
""" | |
from tornado import web | |
from tornado.escape import utf8 | |
import cProfile as profile | |
import settings | |
def exec_patch(execute): | |
def _(self, transforms, *args, **kwargs): | |
if settings.get("profile"): | |
self.profiler = profile.Profile() | |
self.profiler.enable() | |
return execute(self, transforms, *args, **kwargs) | |
return _ | |
def finish_patch(finish): | |
def _(self): | |
if self.profiler: | |
self.profiler.disable() | |
self.profiler.dump_stats("profile/%s.dat" % utf8(self.request.uri)) | |
return finish() | |
return _ | |
old_execute = web.RequestHandler._execute | |
web.RequestHandler._execute = exec_patch(old_execute) | |
old_finish = web.RequestHandler.finish | |
web.RequestHandler.finish = finish_patch(old_finish) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment