gpt4 book ai didi

python - Python 3.2 及更高版本中的 sys.setswitchinterval

转载 作者:太空狗 更新时间:2023-10-29 17:50:23 25 4
gpt4 key购买 nike

Python 3.2 引入了一个 new GIL implementation作者 Antoine Pitrou,它公开了函数 sys.setswitchinterval .

什么时候改变它会有用,为什么?

最佳答案

一个用途是确保操作以原子方式运行,例如:

sw_interval = sys.getswitchinterval()
try:
# Setting the switch interval to a very big number to make sure that their will be no
# thread context switching while running the operations that came after.
sys.setswitchinterval(sys.maxint)
# Expressions run here will be atomic ....
finally:
sys.setswitchinterval(sw_interval)

另一个用例是在您遇到 convoy effect 时专门调整您的代码(或者新 GIL 表现不佳的任何边缘情况)。也许(只是也许)改变上下文切换间隔可以给你更快的速度。

免责声明:上面引用的第一种方法被认为是黑魔法,完全不推荐使用(在该用例中首选 threading.Lock-likes)。一般来说,我不认为改变线程上下文切换间隔是正常情况下要做的事情。我将解释 Tim Peters 已经说过的关于元类的话:改变线程上下文切换间隔比 99% 的人需要的更神奇

关于python - Python 3.2 及更高版本中的 sys.setswitchinterval,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7376776/

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