def main(): monkey.patch_all() tasks = [] gevent_profiler.attach() g = gevent.spawn(eat_up_cpu) g.join() gevent_profiler.detach()
def GET(self): web.header('Content-Type', 'text/html') params = web.input(_sheep_app_server=None, _sheep_profile_count=None, _sheep_pid=None) if not web.ctx.env.get('QUERY_STRING', None): return FORM_HTML % socket.gethostname() count = params._sheep_profile_count backend = params._sheep_app_server pid = params._sheep_pid if not (count and backend): return '''params invaild''' count = int(count) if not pid: pid = os.getpid() raise web.seeother("/_sheep/profile/?_sheep_profile_count=%d&_sheep_app_server=%s&_sheep_pid=%d" % \ (count, backend, pid)) else: pid = int(pid) html = FRESH_HTML num_file = os.path.join('/tmp','%s.%d.counter' % (web.ctx.env['SERVER_NAME'], pid)) stat_file = os.path.join('/tmp', '%s.%d.prof' % (web.ctx.env['SERVER_NAME'], pid)) now = os.path.getsize(num_file) if os.path.exists(num_file) else 0 html = html % (now, count, count, backend.encode('utf8'), pid) if not getattr(SHEEPApplication, 'old_call', None): setattr(SHEEPApplication, 'old_call', SHEEPApplication.__call__) if not os.path.exists(num_file) and os.path.exists(stat_file): return output(stat_file) elif not os.path.exists(num_file): with open(num_file, 'w+') as f: f.write('') gevent_profiler.set_stats_output(stat_file) gevent_profiler.set_trace_output(None) def monkey_call(obj, environ, start_response): if os.path.exists(num_file): with open(num_file, 'a') as f: f.write('1') return obj.old_call(environ, start_response) SHEEPApplication.__call__ = monkey_call gevent_profiler.attach() elif os.path.getsize(num_file) >= count: os.remove(num_file) try: gevent_profiler.detach() except: pass SHEEPApplication.__call__ = SHEEPApplication.old_call return output(stat_file) return html
def stop_profiler(): gevent_profiler.detach()
def __exit__(self, type, value, traceback): gevent_profiler.detach()