分析使用reactor/websockets和线程的应用程序

2024-06-26 14:53:07 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我读到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 project site的示例如下:

^{pr2}$

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

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

问候


Tags: 代码程序应用程序iffactory情况线程thread
1条回答
网友
1楼 · 发布于 2024-06-26 14:53:07

可以通过以下方式使用扭曲轮廓仪:

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

hotshot是一个默认的profiler,您也可以使用cprofile。 或者,可以通过以下方法从python脚本运行twisted:

^{pr2}$

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

hot2shot2calltree profiling_results.txt > calltree_profiling

并打开生成的calltree_配置文件:

kcachegrind calltree_profiling

有一个项目用于分析异步执行时间twisted-theseus 你也可以试试pycharm的工具:thread concurrency

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

reactor.callWhenRunning(your_function, *parameters_list)

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

相关问题 更多 >