Python 强制GIL不要切换螺纹

Python 强制GIL不要切换螺纹,python,multithreading,profiling,gil,Python,Multithreading,Profiling,Gil,我正在分析一些多线程代码。 为了测量执行特定代码段所需的时间,我想强制在该段的线程之间进行切换。如何做到这一点 更新: 假设以下伪代码: some_code_1() make_the_interpreter_not_change_thread() take_start_time() # time critical code_to_profile() # time critical take_end_time() # time critical release_the_interp

我正在分析一些多线程代码。
为了测量执行特定代码段所需的时间,我想强制在该段的线程之间进行切换。如何做到这一点

更新:
假设以下伪代码:

some_code_1()
make_the_interpreter_not_change_thread()
take_start_time()   # time critical
code_to_profile()   # time critical
take_end_time()     # time critical
release_the_interpreter()
some_code_2()

我担心的是解释器会在“时间关键型”评测期间切换线程。

GIL不会切换线程。GIL是一个互斥锁,用于防止多个线程同时执行字节码。因此,为了防止线程切换,您需要查看其他地方

您可以使用非常大的值调用,以防止在分析代码时切换


无论何时调用
sys.setcheckinterval(count)
当前“计数器”都会重置为新值,因此只要调用它,至少
count
字节码指令就不允许线程切换。

请参阅我的问题更新。通过
sys.setcheckinterval()
中放置一个大的数字会使解释器不改变线程()
工作吗?解释器是否更改了python虚拟指令的当前计数,或者新值是否仅在解释器检查的下一个周期生效,这很不幸会在
code\u to\u profile()
期间发生?@Jonathan:函数在调用时重置当前计数。因此,使用大值调用它可以保证不会对那么多字节码指令进行线程切换。