condtion遇到高精度后没有阻塞的python延迟

时间:2018-06-08 09:52:23

标签: python delay python-multithreading blocking

我需要在条件满足后在python中执行一个函数,但是在执行函数之前有一个小的第二个延迟,而不会阻塞循环中的另一个程序,并且延迟必须是高精度的。

有没有办法做到这一点?我尝试过使用threading.Threadthreading.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变量。

0 个答案:

没有答案