有没有一种简单的方法可以判断等待 Python GIL 花费了多少时间? [英] Is there an easy way to tell how much time is spent waiting for the Python GIL?

查看:49
本文介绍了有没有一种简单的方法可以判断等待 Python GIL 花费了多少时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个长期运行的 Python 服务,我想知道等待 GIL 的任何可运行线程(即没有因其他原因被阻塞的线程)累计花费了多少挂钟时间.是否有捷径可寻?例如,也许我可以定期将一些计数器转储到其日志文件中.

I have a long-running Python service and I'd like to know how much cumulative wall clock time has been spent by any runnable threads (i.e., threads that weren't blocked for some other reason) waiting for the GIL. Is there an easy way to do this? E.g., perhaps I could periodically dump some counter to its log file.

我的根本动机是排除 GIL 作为这些长期运行过程中神秘响应延迟的来源.没有特别的理由怀疑 GIL(除了它符合症状),但其他形式的日志记录还没有出现任何结果,所以,如果它很容易,那就是很高兴获得这些信息.

My underlying motivation is to rule out the GIL as a source of mystery response latency from these long-running processes. There is no particular reason to suspect the GIL (other than that it would fit the symptoms), but other forms of logging haven't turned up anything yet, so, if it is easy, it would be nice to have this information.

推荐答案

我认为没有简单的方法.可能有一种笨拙的方法,涉及重建 Python 以遍历 PyThreadState 列表并在每次获取锁时计算线程数,但我怀疑是否值得付出努力!

I don't think there's an easy way. There's probably an awkward way, involving rebuilding Python to traverse the PyThreadState list and count the threads each time the lock is acquired, but I doubt it's worth the effort!

我知道这是一个推测性问题,但如果您甚至适度地担心线程会导致延迟,那么转向多处理模型而不是多线程模型可能是明智之举.由于流程在 Python 中更安全且更具可扩展性,因此它们几乎总是可行的最佳选择.

I know this is a speculative question but if you are even moderately concerned about there being delays caused by threading it may be prudent to move to a multiprocessing model instead of a multithreading model. Since processes are both safer and more scalable in Python they are almost always the best choice if practical.

这篇关于有没有一种简单的方法可以判断等待 Python GIL 花费了多少时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆