我有一个Python中的程序,它将侦听输入信号。但是这可能会等待很长时间,所以我想每隔5秒显示一条消息,上面写着“仍在等待”Python超时计数器?
但是我不希望计数器功能中的延迟时间为1秒当信号被定时时,停止程序听信号,并且不正确的定时会产生不正确的结果。
我这么远到这一点,但整个脚本被延迟1秒,每次$ temp_2增加
#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)
如果从用户程序将阻止采取输入。我认为你需要一个线程来显示消息。 – user3764893