python threading.Timer:我需要保护回调中访问的变量吗?

2 投票
1 回答
960 浏览
提问于 2025-04-16 08:23

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

1 个回答

1

你可以在 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 的回调函数访问的变量。

撰写回答