我正在分析一些多线程的CPython代码。
为了测量执行特定代码段所需的时间,我想强制GIL(全局解释器锁)在该段时间内不在线程之间切换。如何做到这一点?
为了测量执行特定代码段所需的时间,我想强制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_interpreter()
some_code_2()
我的担忧是,在“时间关键”的分析期间,解释器会切换线程。
make_the_interpreter_not_change_thread()
中通过sys.setcheckinterval()
放置一个大数字是否可行?解释器是否会更改Python虚拟指令的当前计数,或者新值只会在下一次解释器检查周期生效,这可能会在code_to_profile()
期间不幸发生? - Jonathan Livni