Python 分析使用反应器/WebSocket和线程的应用程序

Python 分析使用反应器/WebSocket和线程的应用程序,python,autobahn,yappi,Python,Autobahn,Yappi,嗨,我写了一个Python程序,应该在无人值守的情况下运行。它基本上是通过几个线程中的http get请求获取一些数据,并通过websockets和autobahn框架获取数据。运行2天,我发现它的内存需求在不断增长,甚至会在没有任何通知的情况下停止。 文档上说我必须把reactor作为应用程序的最后一行代码运行 我读过一篇文章,它能够分析线程化应用程序 下面是一些伪代码 from autobahn.twisted.websocket import WebSocketClientFactory

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

我读过一篇文章,它能够分析线程化应用程序 下面是一些伪代码

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() 
来自的示例如下所示:

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\u func\u stats().print\u all()
加上
yappi.get\u thread\u stats().print\u all()
放在
reactor.run()
之后,但是因为这个代码从未执行过,所以我永远也不会执行它

那么我该如何描述这样一个程序呢


关于

可以通过以下方式使用扭曲剖面仪:

twistd -n --profile=profiling_results.txt --savestats --profiler=hotshot your_app
hotshot是默认的探查器,您还可以使用cprofile。 或者,您可以通过以下方式从python脚本运行twistd:

from twistd.scripts import run
run()
hot2shot2calltree profiling_results.txt > calltree_profiling
并通过
sys.argv[1:1]=[“--profile=profileing_results.txt”,…]向脚本添加必要的参数。
毕竟,您可以通过以下方式将hotshot格式转换为calltree:

from twistd.scripts import run
run()
hot2shot2calltree profiling_results.txt > calltree_profiling
并打开生成的calltree_评测文件:

kcachegrind calltree_profiling
有一个用于分析异步执行时间的项目 您也可以尝试pycharm的工具:

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

reactor.callWhenRunning(your_function, *parameters_list)
或者通过
reactor.addSystemEventTrigger()
使用事件描述和分析函数调用