Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Stop thread that is waiting for user input

I'm trying to have my script trigger a user input when the user pressed the return key. The main program will then check the txUpdated flag and use this input.

I have a thread running in python that simply waits for user input:

class InputThread(threading.Thread):
    def __init__(self, threadID, name):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.name = name
    def run(self):
        global screenLock
        global txUpdated
        global txMessage
        global endFlag
        lock = threading.Lock()

        print "Starting " + self.name
        while not endFlag:
            txMessage = raw_input()
            if (txMessage == ""):
                screenLock = 1
                txMessage = raw_input("Enter Tx String: ")
                screenLock = 0

                with lock:
                    txUpdated = 1

        print "Exiting " + self.name

The problem is I don't know how to end this thread at any point without receiving a user input. Even if my main program sets the endFlag the thread wont end until the user inputs one more input.

Does anyone have any suggestions on how to accomplish this?

like image 764
spizzak Avatar asked Aug 05 '14 16:08

spizzak


1 Answers

Here is a Windows-only solution, based on this answer by Alex Martelli:

import msvcrt
import time
import threading

endFlag = False

class InputThread(threading.Thread):
    def __init__(self, threadID, name):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.name = name

    def run(self):
        global screenLock
        global txUpdated
        global txMessage
        lock = threading.Lock()
        print "Starting " + self.name
        while not endFlag:
            txMessage = self.raw_input_with_cancel()  # This can be cancelled by setting endFlag
            if (txMessage == ""):
                screenLock = 1
                txMessage = raw_input("Enter Tx String: ")
                screenLock = 0

                with lock:
                    txUpdated = 1

        print "Exiting " + self.name

    def raw_input_with_cancel(self, prompt=None):
        if prompt:
            print prompt,
        result = []
        while True:
            if msvcrt.kbhit():
                result.append(msvcrt.getche())
                if result[-1] in ['\r', '\n']:
                    print
                    return ''.join(result).rstrip()
            if endFlag:
                return None
            time.sleep(0.1)  # just to yield to other processes/threads

When endFlag is set to True, the thread will exit.

like image 123
dano Avatar answered Nov 12 '22 09:11

dano