我有一个相当大的基于扭曲的应用程序。某处有一些代码在主线程中执行了太多计算,导致延迟峰值。我想把它放到后台线程中,但我必须先找到罪魁祸首。
是否已将任何工具扭曲以帮助我追踪主线程的位置?
Twisted应该能够注意到线程是否在几毫秒内没有返回,然后帮我弄清楚是什么让线程忙。例如,通过转储主线程的堆栈跟踪。
答案 0 :(得分:2)
杰夫格里尔有a tool called twisted_hang
,可以告诉你扭曲的进程挂在哪里。
答案 1 :(得分:1)
运行:
twistd -n -y hello_world.tac --profile=pstats_obj --profiler=cprofile --savestats
来自终端的输出:
bob@squids:~/Desktop/toy_problems/twisted$ twistd -n -y hello_world.tac --profile=pstats_obj --profiler=cprofile --savestats
2015-08-17 06:52:23-0500 [-] Log opened.
2015-08-17 06:52:23-0500 [-] twistd 14.0.2 (/usr/bin/python 2.7.9) starting up.
2015-08-17 06:52:23-0500 [-] reactor class: twisted.internet.epollreactor.EPollReactor.
2015-08-17 06:52:23-0500 [-] Site starting on 8000
2015-08-17 06:52:23-0500 [-] Starting factory <twisted.web.server.Site instance at 0x7f81e54002d8>
2015-08-17 06:52:35-0500 [HTTPChannel,0,127.0.0.1] "127.0.0.1" - - [17/Aug/2015:11:52:34 +0000] "GET / HTTP/1.1" 200 53 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.155 Safari/537.36"
2015-08-17 06:52:35-0500 [HTTPChannel,0,127.0.0.1] "127.0.0.1" - - [17/Aug/2015:11:52:35 +0000] "GET /favicon.ico HTTP/1.1" 200 64 "http://localhost:8000/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.155 Safari/537.36"
^C2015-08-17 06:52:40-0500 [-] Received SIGINT, shutting down.
2015-08-17 06:52:40-0500 [-] (TCP Port 8000 Closed)
2015-08-17 06:52:40-0500 [-] Stopping factory <twisted.web.server.Site instance at 0x7f81e54002d8>
2015-08-17 06:52:40-0500 [-] Main loop terminated.
2015-08-17 06:52:40-0500 [-] Server Shut Down.
bob@squids:~/Desktop/toy_problems/twisted$
然后:
import pstats
s = pstats.Stats('pstats_obj')
s.sort_stats('time')
或者您可以在没有--savestats
的情况下投放:twistd -n -y hello_world.tac --profile=pstats_obj --profiler=cprofile
508 function calls (501 primitive calls) in 0.036 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
8 0.000 0.000 0.000 0.000 :0(abs)
4 0.000 0.000 0.000 0.000 :0(acquire)
2 0.000 0.000 0.000 0.000 :0(add)
15 0.000 0.000 0.000 0.000 :0(append)
12 0.000 0.000 0.000 0.000 :0(callable)
2 0.000 0.000 0.000 0.000 :0(clear)
6 0.000 0.000 0.000 0.000 :0(copy)
8 0.000 0.000 0.000 0.000 :0(fcntl)
3 0.000 0.000 0.000 0.000 :0(fileno)
4 0.000 0.000 0.000 0.000 :0(flush)
4 0.000 0.000 0.000 0.000 :0(fromtimestamp)
2 0.000 0.000 0.000 0.000 :0(get)
9 0.000 0.000 0.000 0.000 :0(get_ident)
5 0.000 0.000 0.000 0.000 :0(getattr)
1 0.000 0.000 0.000 0.000 :0(getsignal)
2 0.000 0.000 0.000 0.000 :0(gmtime)
2 0.000 0.000 0.000 0.000 :0(hasattr)
3 0.000 0.000 0.000 0.000 :0(heappop)
4 0.000 0.000 0.000 0.000 :0(heappush)
21 0.000 0.000 0.000 0.000 :0(isinstance)
1 0.000 0.000 0.000 0.000 :0(iter)
4 0.000 0.000 0.000 0.000 :0(join)
18 0.000 0.000 0.000 0.000 :0(len)
4 0.000 0.000 0.000 0.000 :0(map)
5 0.000 0.000 0.000 0.000 :0(max)
5 0.000 0.000 0.000 0.000 :0(min)
1 0.000 0.000 0.000 0.000 :0(pipe)
5 0.000 0.000 0.004 0.001 :0(poll)
16 0.000 0.000 0.000 0.000 :0(pop)
4 0.000 0.000 0.000 0.000 :0(range)
2 0.000 0.000 0.000 0.000 :0(read)
1 0.000 0.000 0.000 0.000 :0(register)
4 0.000 0.000 0.000 0.000 :0(release)
# from twisted.internet import reactor
from twisted.application import service, internet
from twisted.web import static, server
from twisted.web.resource import Resource
class Hello(Resource):
def getChild(self, name, request):
return self
def render_GET(self, request):
return '<html>Hello, GET world! I am located at %r. </html>' \
% (request.prepath)
def getWebService():
"""
Return a service suitable for creating an application object.
"""
site = server.Site(Hello())
# site = server.Site(Hello())
# reactor.listenTCP(8000, site)
# reactor.run()
return internet.TCPServer(8000, site)
# this is the core part of any tac file, the creation of the root-level
# application object
application = service.Application("Hello application")
# attach the service to its parent application
service = getWebService()
service.setServiceParent(application)
这是&#34; hello_word.tac&#34;:
# from twisted.internet import reactor
from twisted.web import static, server
from twisted.web.resource import Resource
from twisted.application import service, internet
class Hello(Resource):
def getChild(self, name, request):
return self
def render_GET(self, request):
return '<html>Hello, GET world! I am located at %r. </html>' \
% (request.prepath)
def getWebService():
"""
Return a service suitable for creating an application object.
This service is a simple web server that serves files on port 8080 from
underneath the current working directory.
"""
site = server.Site(Hello())
return internet.TCPServer(8000, site)
# this is the core part of any tac file, the creation of the root-level
# application object
application = service.Application("Hello application")
# attach the service to its parent application
service = getWebService()
service.setServiceParent(application)
一旦分析了,那么你可以添加一个装饰器来添加某种类型的钩子(逐行)来分析占用大量时间的调用。