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

标签 python multithreading timer thread-safety

我正在尝试使用 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 的回调访问的变量。

关于 python threading.Timer : do i need to protect the variable accessed inside the callback?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4417058/

相关文章:

Python 惰性枚举

python - numpy atleast_3d() 的行为

Java的TimerTask run()函数在内部调用trait函数时未调用

python时间一试除了

python - PyCharm 中未使用的错误导入语句?

python - 在 python 中使用带有 .format 的元组

multithreading - Perl 线程安全模块

c# - 异步/等待示例

multithreading - 关于互斥量和信号量

java - 如何在java中固定一个方法的特定时间