1

嗨,我写了一个应该无人值守运行的 Python 程序。它的基本作用是通过几个线程中的 http get 请求获取一些数据,并通过 websockets 和高速公路框架获取数据。运行它 2 天表明它的内存需求不断增长,甚至在没有任何通知的情况下停止。文档说我必须将反应器作为应用程序中的最后一行代码运行。

我读到yappi能够分析线程应用程序这是一些伪代码

from autobahn.twisted.websocket import  WebSocketClientFactory,connectWS

if __name__ == "__main__":
#setting up a thread

#start the thread
Consumer.start()

xfactory = WebSocketClientFactory("wss://url")
cex_factory.protocol = socket
## SSL client context: default
##
if factory.isSecure:
    contextFactory = ssl.ClientContextFactory()
else:
    contextFactory = None

connectWS(xfactory, contextFactory)

reactor.run() 

来自yappi 项目站点的示例如下:

import yappi
def a(): 
    for i in range(10000000): pass

yappi.start()
a()
yappi.get_func_stats().print_all()
yappi.get_thread_stats().print_all()

所以我可以把它放在yappi.start()开头和yappi.get_func_stats().print_all()后面yappi.get_thread_stats().print_all()reactor.run()但由于这段代码永远不会执行,我永远不会执行它。

那么我如何分析这样的程序呢?

问候

4

1 回答 1

0

可以通过以下方式使用扭曲分析器:

twistd -n --profile=profiling_results.txt --savestats --profiler=hotshot your_app

hotshot 是一个默认的分析器,你也可以使用 cprofile。或者,您可以通过以下方式从您的 python 脚本运行 twistd:

from twistd.scripts import run
run()

并将必要的参数添加到脚本中,sys.argv[1:1] = ["--profile=profiling_results.txt", ...] 毕竟您可以通过以下方式将 hotshot 格式转换为 calltree:

hot2shot2calltree profiling_results.txt > calltree_profiling

并打开生成的 calltree_profiling 文件:

kcachegrind calltree_profiling

有一个用于分析异步执行时间的项目twisted-theseus 也可以试试pycharm的工具:线程并发

这里有一个相关的问题 也可以通过以下方式运行你的函数:

reactor.callWhenRunning(your_function, *parameters_list)

或者通过reactor.addSystemEventTrigger()事件描述和您的分析函数调用。

于 2015-11-20T13:06:18.597 回答