gpt4 book ai didi

python - 分析 GIL

转载 作者:太空宇宙 更新时间:2023-11-03 14:56:15 26 4
gpt4 key购买 nike

有没有办法剖析 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com