python - 分析 GIL

标签 python multithreading profiling cython gil

有没有办法剖析 python 进程对 GIL 的使用情况?基本上,我想知道持有 GIL 的时间百分比。该进程是单线程的。

我的动机是我有一些用 Cython 编写的代码,它使用 nogil。理想情况下,我想在多线程进程中运行它,但为了知道这是否可能是个好主意,我需要知道 GIL 是否在大量时间是空闲的。


我找到了 this related question ,从8年前开始。唯一的答案是“否”。希望从那以后情况有所改变。

最佳答案

完全是偶然的,我发现了一个工具可以做到这一点:gil_load .

它实际上是在我发布问题后发布的。

做得好,@chrisjbillington。

>>> import sys, math
>>> import gil_load
>>> gil_load.init()
>>> gil_load.start(output = sys.stdout)
>>> for x in range(1, 1000000000):
...     y = math.log(x**math.pi)
[2017-03-15 08:52:26]  GIL load: 0.98 (0.98, 0.98, 0.98)
[2017-03-15 08:52:32]  GIL load: 0.99 (0.99, 0.99, 0.99)
[2017-03-15 08:52:37]  GIL load: 0.99 (0.99, 0.99, 0.99)
[2017-03-15 08:52:43]  GIL load: 0.99 (0.99, 0.99, 0.99)
[2017-03-15 08:52:48]  GIL load: 1.00 (1.00, 1.00, 1.00)
[2017-03-15 08:52:52]  GIL load: 1.00 (1.00, 1.00, 1.00)
<...>

>>> import sys, math
>>> import gil_load
>>> gil_load.init()
>>> gil_load.start(output = sys.stdout)
>>> for x in range(1, 1000000000):
...     with open('/dev/null', 'a') as f:
...         print(math.log(x**math.pi), file=f)

[2017-03-15 08:53:59]  GIL load: 0.76 (0.76, 0.76, 0.76)
[2017-03-15 08:54:03]  GIL load: 0.77 (0.77, 0.77, 0.77)
[2017-03-15 08:54:09]  GIL load: 0.78 (0.78, 0.78, 0.78)
[2017-03-15 08:54:13]  GIL load: 0.80 (0.80, 0.80, 0.80)
[2017-03-15 08:54:19]  GIL load: 0.81 (0.81, 0.81, 0.81)
[2017-03-15 08:54:23]  GIL load: 0.81 (0.81, 0.81, 0.81)
[2017-03-15 08:54:28]  GIL load: 0.81 (0.81, 0.81, 0.81)
[2017-03-15 08:54:33]  GIL load: 0.80 (0.80, 0.80, 0.80)
<...>

关于python - 分析 GIL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42378491/

相关文章:

python - SSL:来自 Ubuntu 16.0.4 中的 Python pip 的 CERTIFICATE_VERIFY_FAILED 错误

python - 如何使用 Django 创建短 uuid

c# - 是否可以使用哈希表作为锁定对象?

c++ - 多线程程序中捕获SIGSEGV、SIGFPE等信号

python - sqlite3 或 CSV 文件

Python pandas "filter"仅交易日的时间序列

ios - GCD 串行队列和竞争条件

c++ - 需要帮助破译 gprof 输出

ruby - 有没有好的 ruby​​ 线路分析器?

performance - 与C/C++最相关的性能指标