6

In the WHILE loop, I wanna run two function, one is base function, which will run everytime, the other is user_input function, when user input 'disarm', program can run user_input function. This two function need in WHILE loop so can run all the time.

How could I do to write a function to accomplish this?

Because its realtime so I cant add time.sleep in threading.

Thanks.

import threading

class BackInput(threading.Thread):
    def __init__(self):
        super(BackInput, self).__init__()


    def run(self):
        self.input = raw_input()

while True:
    threading1 = BackInput()
    threading1.start()
    threading1.join()
    if threading1.input == 'disarm':
        print 'Disarm'
        break
    print 'Arm'

In this code, the program should print Arm every second, when I typed disarm, it can print Disarm and break it.

VValkyrja
  • 73
  • 1
  • 1
  • 7
  • 1
    Does this answer your question? [waiting for user input in separate thread](https://stackoverflow.com/questions/12078363/waiting-for-user-input-in-separate-thread) – ggorlen Feb 17 '21 at 02:23

1 Answers1

10

You really need to be more specific. Why do these need to be in threads? You should show us what you have tried, or describe in more detail what you are trying to accomplish.

In your current setup, you are putting the thread inside a loop, so it can't run independently of each user input.

edited: here is some cleaned up code as an example for you, based on your post edits and comments.

import threading
import time
import sys

def background():
    while True:
        time.sleep(3)
        print 'disarm me by typing disarm'


def other_function():
    print 'You disarmed me! Dying now.'

# now threading1 runs regardless of user input
threading1 = threading.Thread(target=background)
threading1.daemon = True
threading1.start()

while True:
    if raw_input() == 'disarm':
        other_function()
        sys.exit()
    else:
        print 'not disarmed'
Michael Neylon
  • 584
  • 4
  • 14
  • 1
    Hi, thanks for your advice. I wanna run two function in the while loop, one is my base function, which will run all the time, the other function is input function, when user input disarm, program will run input function, else program still run base function. how could I accomplish this use python? Thanks:) – VValkyrja Aug 02 '15 at 06:07
  • 3
    You put it in a loop, it will run each time the loop runs, and in your example you start a thread, joined it back, then asked for user input. You aren't running anything 'all the time' in your current example. If you want a function to run the entire time the program is running, and be able to accept user input to run another function, do like I did in my second example. It will start the other thread immediately and it will keep running, in this example it will run even on any user input, until they type the specified 'disarm' which runs the other_function() then closes the program. – Michael Neylon Aug 02 '15 at 06:21
  • 1
    Thanks for your patient advice. I have tested your second and add print 'arm' in the BackInput, it seems only print arm one time, is it all time running? – VValkyrja Aug 02 '15 at 06:30
  • That's because you only printed once. See my edits based on your post edits and comments. It's clearer to use the threading constructor and here I show you with while loop and time.sleep() that it can run at the same time as any user input. – Michael Neylon Aug 02 '15 at 06:58
  • I have to say, this is the clearest example of multi-threading and input I have seen. Bravo – netrate Mar 13 '22 at 20:00