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

问题描述 投票:12回答:1

Python 3.2引入了Antoine Pitrou的new GIL implementation,它公开了函数sys.setswitchinterval

何时更改此功能很有用,为什么?

python multithreading python-3.x scheduling gil
1个回答
5
投票

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

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) 时(或在新GIL产生不良性能的任何极端情况下)专门调整代码。也许(只是也许)更改上下文切换间隔可以提高速度。

Disclaimer:上面引用的第一种方法被认为是黑魔法,完全不建议使用(在这种情况下,首选convoy effect类)。通常,我认为在正常情况下不必更改线程上下文切换间隔。我将解释蒂姆·彼得斯已经对元类说的话:threading.Lock

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.