gpt4 book ai didi

python - threading.timer 在 python 中如何工作

转载 作者:行者123 更新时间:2023-12-01 03:18:00 26 4
gpt4 key购买 nike

我想每隔 n 运行一个函数秒。经过一番研究,我找到了这段代码:

import threading

def print_hello():
threading.Timer(5.0, print_hello).start()
print("hello")

print_hello()

print_hello() 时,是否每 5 秒创建一个新线程被称为?

最佳答案

Timer 是一个线程。它是在实例化 Timer() 时创建的。该线程等待给定的时间,然后调用该函数。由于该函数创建了一个新的计时器,所以它每 5 秒调用一次。

关于python - threading.timer 在 python 中如何工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42290613/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com