3

I have a program in python, which will be listening for an input signal. But this could be a very long time that it waits, so I want to have a message that is displayed every 5 seconds that just says "still waiting"

But I don't want the delay 1 second in the counter function to stop the program from listening to the signal, as the signal is timed, and an incorrect timing will produce an incorrect result.

I've so far got to this, but the whole script is delayed by 1 second each time $temp_2 is increased

#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

The following is my timeout_counter() function:

def timeout_counter(temp_2):
    temp_2 = temp_2 + 1
    time.sleep(1)
    return (temp_2)
1
  • If you are taking input from user the program will block. I think you'd need a thread to display the message. Commented Jun 20, 2017 at 13:44

1 Answer 1

1

Instead of using time.sleep, you could just comapre the timestamp at a given iteration and the timestamp from the previous one, using time.time().

Your algorithm should look like that :

#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()

Your timeout_counter() function is now useless :)

Sign up to request clarification or add additional context in comments.

4 Comments

"I want to have a message that is displayed every 5 seconds" I think you misread the question ;)
You would be right indeed, if he wanted to wait for 5 minutes ;)
As I mentioned in the comment above, if the SO is taking input from user the program would block, and a thread might be needed in this case. Still the code from SO is incomplete, so I cant say much.
@ThomasDussaut Thanks, this has sorted it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.