Python超时计数器?

Python超时计数器?,python,multiprocessing,Python,Multiprocessing,我有一个python程序,它将监听输入信号。但它等待的时间可能很长,所以我希望每5秒显示一条消息,上面写着“仍在等待” 但是我不希望计数器功能中的延迟1秒阻止程序收听信号,因为信号是定时的,不正确的定时将产生不正确的结果 到目前为止,我已经做到了这一点,但是每增加$temp_2,整个脚本就会延迟1秒 #If option number 1 was selected, proceed if(int(input_string) == 1): input_string = "" tem

我有一个python程序,它将监听输入信号。但它等待的时间可能很长,所以我希望每5秒显示一条消息,上面写着“仍在等待”

但是我不希望计数器功能中的延迟1秒阻止程序收听信号,因为信号是定时的,不正确的定时将产生不正确的结果

到目前为止,我已经做到了这一点,但是每增加$temp_2,整个脚本就会延迟1秒

#If option number 1 was selected, proceed
if(int(input_string) == 1):
    input_string = ""
    temp_2 = 0
    print('Currently listening for messages. System still working. Please wait.')

        while True:
            if(input_0 == False):
                input_string = input_string + "0"
                temp_1 = temp_1 + "0"

            if(input_1 == False):
                input_string = input_string + "1"
                temp_1 = temp_1 + "1"

            if(len(input_string) ==  8):
                output_string = output_string + chr(int(input_string, 2))
                input_string = ""

                if(len(temp_1) == 40):
                    if(temp_1 == "0011110001100101011011100110010000111110"):
                        print('Received terminator!')
                    else:
                        temp_1 = temp_1[8::]

                #increase the counter, but don't stop the script from
                #listening for the input. can it be done? 
                temp_2 = timeout_counter(temp_2)

                print(temp_2)

                if(temp_2 == 5):
                    print('still working. not broken.')
                    temp_2 = 0
下面是我的timeout_counter()函数:

def timeout_counter(temp_2):
    temp_2 = temp_2 + 1
    time.sleep(1)
    return (temp_2)

不使用time.sleep,只需使用time.time()将给定迭代的时间戳与上一次迭代的时间戳对齐即可

您的算法应该如下所示:

#If option number 1 was selected, proceed
if(int(input_string) == 1):
    input_string = ""
    temp_2 = time.time()
    print('Currently listening for messages. System still working. Please wait.')

        while True:
            if(input_0 == False):
                input_string = input_string + "0"
                temp_1 = temp_1 + "0"

            if(input_1 == False):
                input_string = input_string + "1"
                temp_1 = temp_1 + "1"

            if(len(input_string) ==  8):
                output_string = output_string + chr(int(input_string, 2))
                input_string = ""

                if(len(temp_1) == 40):
                    if(temp_1 == "0011110001100101011011100110010000111110"):
                        print('Received terminator!')
                    else:
                        temp_1 = temp_1[8::]

                #increase the counter, but don't stop the script from
                #listening for the input. can it be done? 
                temp_2 = timeout_counter(temp_2)

                print(temp_2)

                if(time.time() - temp_2 >= 5.):
                    print('still working. not broken.')
                    temp_2 = time.time()

您的timeout\u counter()函数现在没有用了:)

“我想每5秒显示一条消息”我想您误读了问题;)如果他想等5分钟,你的确是对的;)正如我在上面的评论中提到的,如果SO从用户那里获取输入,程序将阻塞,在这种情况下可能需要一个线程。SO的代码仍然不完整,所以我不能说太多。@ThomasDussaut谢谢,这已经对它进行了排序。如果您从用户处获取输入,程序将被阻止。我想你需要一个线程来显示消息。