break/interrupt a time.sleep() in python

后端 未结 6 1356
猫巷女王i
猫巷女王i 2020-12-01 03:30

I need to break from time.sleep() using ctrl c.

While 1:
    time.sleep(60)

In the above code when the control enters time.sleep function a

相关标签:
6条回答
  • 2020-12-01 03:45

    Not sure what the sense of this code is - but if necessary use a shorter sleep() interval and put a for loop around it:

    for i in range(60):
       sleep(1)
    

    Catching the KeyboardInterrupt exception using try..except is straight-forward

    0 讨论(0)
  • 2020-12-01 03:52

    The correct answer is to use python stdlib's threading.Event

    Sure you can tune down your sleep interval so you sleep for very short periods, but what if you actually want to run your loop once every 60s? Then you need to do more work to determine if it's time to run or just keep sleeping. Furthermore, you're still technically blocking but for only a short period of time. Contrast to threading.Event:

    from threading import Event
    
    exit = Event()
    
    def main():
        while not exit.is_set():
          do_my_thing()
          exit.wait(60)
    
        print("All done!")
        # perform any cleanup here
    
    def quit(signo, _frame):
        print("Interrupted by %d, shutting down" % signo)
        exit.set()
    
    if __name__ == '__main__':
    
        import signal
        for sig in ('TERM', 'HUP', 'INT'):
            signal.signal(getattr(signal, 'SIG'+sig), quit);
    
        main()
    

    When the signal handler calls exit.set(), the main thread's wait() call will immediately be interrupted.

    Now, you could use an Event to signal that there's more work to do, etc. But in this case it does double duty as a convenient indicator that we want to quit (e.g. the while not exit.is_set() part.)

    You also have the option to put any cleanup code after your while loop.

    0 讨论(0)
  • 2020-12-01 03:54

    Figured I'd throw this in.

    import time
    
    
    def sleep(seconds):
        for i in range(seconds):
            try:
                time.sleep(1)
            except KeyboardInterrupt:
                print("Oh! You have sent a Keyboard Interrupt to me.\nBye, Bye")
                break
    
    
    sleep(60)
    
    0 讨论(0)
  • 2020-12-01 03:55

    I tried your code with python versions 2.5, 2.6, 3 under Linux and all throw "KeyboardInterrupt" exception when hitting CTRL-C.

    Maybe some exception handling catches the Interrupt or your problem is like this: Why is KeyboardInterrupt not working in python?

    0 讨论(0)
  • 2020-12-01 04:01

    The KeyboardInterrupt exception is raised when a user hits the interrupt key, Ctrl-C. In python this is translated from a SIGINT signal. That means, you can get handle it however you want using the signal module:

    import signal
    
    def handler(signum, frame):
        print("do whatever, like call thread.interrupt_main()")
    
    signal.signal(signal.SIGINT, handler)
    print("Waiting for SIGINT...")
    signal.pause()
    

    That way, you can do whatever you want at the receipt of a keyboard interrupt.

    0 讨论(0)
  • 2020-12-01 04:06

    The most elegant solution is certainly threading.Event, though if you only need a quick hack, this code works well :

    import time
    
    def main():
        print("It’s time !")
    
    if __name__ == "__main__":
        print("press ctrl-c to stop")
        loop_forever = True
        while loop_forever:
            main()
            try:
                time.sleep(60)
            except KeyboardInterrupt:
                loop_forever = False
    
    0 讨论(0)
提交回复
热议问题