Skip to main content
 首页 » 编程设计

python threading.Timer : do i need to protect the variable accessed inside the callback

2025年05月04日48soundcode

我正在尝试使用 threading.Timer,我想知道这个计时器是否在另一个线程中启动?所以我需要保护这个定时器访问的变量?

请您参考如下方法:

您可以在 threading.py 中看到归档 threading.Timer()返回一个新的 Thread 实例。或者你可以运行一个例子:

import threading 
 
def ontimer(): 
    print threading.current_thread() 
 
def main(): 
    threading.Timer(2, ontimer).start() 
    print threading.current_thread() 
 
if __name__=="__main__": 
    main() 

您需要保护 Timer 的回调访问的变量。