【发布时间】:2022-12-24 22:55:28
【问题描述】:
这是我想调用的函数:
def server_fn():
#the code below this line is the code I wanna run every 0.2s and stop after a total of 5s
frame = url_to_image('http://192.168.180.161/1600x1200.jpg')
ans = get_model_output(frame)
if ans == 1 :
url = 'http://192.168.180.161/post/1'
else:
url = 'http://192.168.180.161/post/0'
response = requests.get(url)
print(response.content)
每次调用 server_fn() 时,我希望它在 5 秒内运行该代码 25 次。我该怎么做?
我试过这个:
import threading
def printit():
thread = threading.Timer(1.0, printit)
thread.start()
x = 0
if x == 10:
thread.cancel()
else:
x += 1
print(x)
printit()
但输出只会永远显示每行 1,并且不会停止。这只是我想运行的一个测试函数,以查看该函数是否按预期运行。
【问题讨论】:
-
您正在传递给此计时器的函数中初始化
Timer()。这会导致递归和不受控制的线程垃圾邮件。
标签: python multithreading timer