Python Forum
input interrupt - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: input interrupt (/thread-31376.html)



input interrupt - Nickd12 - Dec-07-2020

how can i get this to just interrupt the input and continue down the while true loop. instead if there is no input after 3 seconds it end the program.

class FiveSec(threading.Thread):
    def restart(self):
        self.my_timer = time.time() + 3

    def run(self, *args):

        self.restart()
        while 1:
            time.sleep(0.1)
            if time.time() >= self.my_timer:
                break
        os.kill(os.getpid(), signal.SIGINT)




def main():

    try:

        t = FiveSec()
        t.daemon = True
        t.start()
        x = input('::> ')
        return x

    except KeyboardInterrupt:
        print("\nDone!")

while True:
    if "hi" in main():
        print("hi")


    print("hello")
    x = 2+2
    print(x)



RE: input interrupt - Gribouillis - Dec-09-2020

Here is a solution I wrote two years ago. I hope it helps
import selectors
import sys
 
def main():
    sel = selectors.DefaultSelector()
    sel.register(sys.stdin, selectors.EVENT_READ)
    print("Enter passcode: ", end='')
    sys.stdout.flush()
    pairs = sel.select(timeout=5)
    if pairs:
        passcode = sys.stdin.readline().strip()
        print('you entered:', passcode)
    else:
        print('\ntimed out')
 
if __name__ == '__main__':
    main()