我需要在条件满足后在python中执行一个函数,但是在执行函数之前有一个小的第二个延迟,而不会阻塞循环中的另一个程序,并且延迟必须是高精度的。
有没有办法做到这一点?我尝试过使用threading.Thread
和threading.Timer
,但延迟并不准确。这是我的代码:
count = 2500
delay = 0.5 # delay before executing function
while True:
# Another program here
.......
if(count <= 2000):
if(found == 0): # Run once until count >= 2000 again
# Execute pressKey function
startThread = threading.Timer(delay, pressKey).start()
found = 1
if(count >= 2000):
if(found == 1): # Run once until count <= 2000 again
# Execute releaseKey function
endThread = threading.Timer(delay, releaseKey).start()
found = 0
假设变量count
正在减少直到0并且逐渐增加到2500,并且if condition
不仅仅是我提到的那两个,还有6个if condition
类似于代码上面有不同的count
变量。