完全偶然,我发现了一个可以执行此操作的工具:gil_load https://github.com/chrisjbillington/gil_load.
原来已经发布了after我发布了问题。
干得好,@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)
<...>