From 02bf3b47b38f5a3c4894719139fd44188bdea6f9 Mon Sep 17 00:00:00 2001 From: AN Long Date: Fri, 10 Dec 2021 18:19:31 +0800 Subject: [PATCH] Compatible with gevent --- pypprof/net_http.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/pypprof/net_http.py b/pypprof/net_http.py index da38f77..312de8e 100644 --- a/pypprof/net_http.py +++ b/pypprof/net_http.py @@ -3,9 +3,9 @@ import gc import pkg_resources import sys -import threading import time import traceback +from threading import active_count, Thread import six from six.moves.BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer @@ -22,6 +22,13 @@ from pypprof.builder import Builder from pypprof import thread_profiler +try: + from gevent import monkey + active_count = monkey.get_original("threading", "active_count") + Thread = monkey.get_original("threading", "Thread") +except ImportError: + pass + _wall_profiler = WallProfiler() @@ -41,7 +48,7 @@ def start_pprof_server(host='localhost', port=8080): _wall_profiler.register_handler() server = HTTPServer((host, port), PProfRequestHandler) - bg_thread = threading.Thread(target=server.serve_forever) + bg_thread = Thread(target=server.serve_forever) bg_thread.daemon = True bg_thread.start() return server @@ -80,7 +87,7 @@ def do_GET(self): def index(self): template = pkg_resources.resource_string(__name__, "index.html").decode("utf-8") - body = template.format(num_threads=threading.active_count()) + body = template.format(num_threads=active_count()) self.send_response(200) self.send_header("X-Content-Type-Options", "nosniff")