Skip to content

Instantly share code, notes, and snippets.

@mynameisfiber
Forked from wayhome/tornado_patch.py
Last active December 17, 2015 22:09
Show Gist options
  • Save mynameisfiber/5680215 to your computer and use it in GitHub Desktop.
Save mynameisfiber/5680215 to your computer and use it in GitHub Desktop.
#!/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