我想test_func
分开工作。它应该每 N 秒打印一次,我已经试过了,但是 time.sleep() 只是停止了我的程序。
import time
import threading
def test_func():
time.sleep(10)
print("Printing stuff....")
t = threading.Timer(10, test_func).start()
test_func()
# something goes here constantly
所以我想做这样的事情,但这也不起作用。
import time
import threading
def test_func():
time.sleep(10)
print("Printing stuff....")
# something goes here constantly
while True:
t = threading.Timer(10, test_func).start()
那么,如何修复呢?
为此,您不需要计时器。只需将您的函数放在一个线程中,因此:
def test_func():
while True:
print("Printing stuff....")
time.sleep(10)
threading.Thread(target=test_func).start()
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句