【发布时间】:2011-11-14 15:52:45
【问题描述】:
Python 3.2 引入了 Antoine Pitrou 的 new GIL implementation,它公开了函数 sys.setswitchinterval。
什么时候改变它会有用,为什么?
【问题讨论】:
-
没有人接受?! 50 次代表一次...
标签: python multithreading python-3.x scheduling gil
Python 3.2 引入了 Antoine Pitrou 的 new GIL implementation,它公开了函数 sys.setswitchinterval。
什么时候改变它会有用,为什么?
【问题讨论】:
标签: python multithreading python-3.x scheduling gil
一种用途是确保操作以原子方式运行,例如:
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 已经说过的关于元类的内容:changing thread context switch interval is deeper magic than 99% of people are going to need。
【讨论】: