Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
input interrupt
#1
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)
Reply
#2
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()
Larz60+ likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  add interrupt for next task kucingkembar 0 751 Oct-07-2022, 12:15 PM
Last Post: kucingkembar
  Determine if keyboard interrupt versus SIGINT trapped? Jibunnokage 5 1,705 Sep-30-2022, 06:54 AM
Last Post: Gribouillis
  Enabling interrupt on Adafruits button/led board Moris526 0 1,984 Apr-30-2021, 03:29 PM
Last Post: Moris526
  python delay without interrupt the whole code Nick_tkinter 4 5,050 Feb-22-2021, 10:51 PM
Last Post: nilamo
  Adafruits Neotrellis interrupt with RAsp and Python Moris526 5 3,476 Jan-01-2021, 11:43 PM
Last Post: Moris526
  Interrupt for Adafruits Neotrellis button/led board Moris526 0 1,768 Dec-28-2020, 05:42 AM
Last Post: Moris526
  Interrupt/Break Function sdprelude 2 6,002 Feb-17-2017, 12:19 AM
Last Post: Ofnuts
  interrupt serial port trainee1 9 18,742 Feb-15-2017, 08:19 AM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020