Re: KeyboardInterrupt

Discussion in 'Python' started by Jon Clements, Dec 10, 2009.

  1. Jon Clements

    Jon Clements Guest

    On Dec 9, 11:53 pm, mattia <> wrote:
    > Hi all, can you provide me a simple code snippet to interrupt the
    > execution of my program catching the KeyboardInterrupt signal?
    >
    > Thanks,
    > Mattia


    Errr, normally you can just catch the KeyboardInterrupt exception --
    is that what you mean?

    Jon.
    Jon Clements, Dec 10, 2009
    #1
    1. Advertising

  2. Jon Clements

    Brad Harms Guest

    On Thu, 10 Dec 2009 00:29:45 +0000, mattia wrote:

    > Il Wed, 09 Dec 2009 16:19:24 -0800, Jon Clements ha scritto:
    >
    >> On Dec 9, 11:53 pm, mattia <> wrote:
    >>> Hi all, can you provide me a simple code snippet to interrupt the
    >>> execution of my program catching the KeyboardInterrupt signal?
    >>>
    >>> Thanks,
    >>> Mattia

    >>
    >> Errr, normally you can just catch the KeyboardInterrupt exception -- is
    >> that what you mean?
    >>
    >> Jon.

    >
    > Ouch, so the simplest solution is just insert in the 'main' function a
    > try/catch? I believed there was the necessity to create a signal and
    > than attach the KeyboardInterrupt to it...



    KeyboardInterrupt is just an exception that gets raised when CTLR+C (or
    the OS's equivalent keyboard combo) gets pressed. It can occur at any
    point in a script since you never know when the user will press it, which
    is why you put the try: except KeyboardInterrupt: around as much of your
    script as possible. The signal that the OS sends to the Python
    interpreter is irrelevant.


    --
    Brad Harms -- http://alphaios.net
    Brad Harms, Dec 10, 2009
    #2
    1. Advertising

  3. mattia wrote:
    > Il Thu, 10 Dec 2009 04:56:33 +0000, Brad Harms ha scritto:
    >
    >> On Thu, 10 Dec 2009 00:29:45 +0000, mattia wrote:
    >>
    >>> Il Wed, 09 Dec 2009 16:19:24 -0800, Jon Clements ha scritto:
    >>>
    >>>> On Dec 9, 11:53 pm, mattia <> wrote:
    >>>>> Hi all, can you provide me a simple code snippet to interrupt the
    >>>>> execution of my program catching the KeyboardInterrupt signal?
    >>>>>
    >>>>> Thanks,
    >>>>> Mattia
    >>>> Errr, normally you can just catch the KeyboardInterrupt exception --
    >>>> is that what you mean?
    >>>>
    >>>> Jon.
    >>> Ouch, so the simplest solution is just insert in the 'main' function a
    >>> try/catch? I believed there was the necessity to create a signal and
    >>> than attach the KeyboardInterrupt to it...

    >>
    >> KeyboardInterrupt is just an exception that gets raised when CTLR+C (or
    >> the OS's equivalent keyboard combo) gets pressed. It can occur at any
    >> point in a script since you never know when the user will press it,
    >> which is why you put the try: except KeyboardInterrupt: around as much
    >> of your script as possible. The signal that the OS sends to the Python
    >> interpreter is irrelevant.

    >
    > Ok, so can you tell me why this simple script doesn't work (i.e. I'm not
    > able to catch the keyboard interrupt)?
    >
    > import time
    > import sys
    > from threading import Thread
    >
    > def do_work():
    > for _ in range(1000):
    > try:
    > time.sleep(1)
    > print(".", end="")
    > sys.stdout.flush()
    > except KeyboardInterrupt:
    > sys.exit()
    >
    > def go():
    > threads = [Thread(target=do_work, args=()) for _ in range(2)]
    > for t in threads:
    > t.start()
    > for t in threads:
    > t.join()
    >
    > go()


    Only the main thread can receive the keyboard interrupt.
    Matthew Barnett, Dec 10, 2009
    #3
  4. Jon Clements

    Lie Ryan Guest

    On 12/11/2009 10:43 AM, mattia wrote:
    > Ok, so is there any way to stop all the threads if the keyboard interrupt
    > is received?


    You can't stop a thread from outside. The thread has to end itself (by
    ending the function). Usually, in the thread, you will check the value
    of a variable. If it's false, then it's time to stop and head to the end
    of the function.

    # global, or better use a Quit sentinel in a Queue
    import time, sys
    from threading import Thread
    running = True

    def do_work():
    while True:
    time.sleep(1)
    print(".", end="")
    sys.stdout.flush()
    if running == False:
    break
    print('thread ended')

    def go():
    global running
    threads = [Thread(target=do_work, args=()) for _ in range(2)]
    for t in threads:
    t.start()
    try:
    while running:
    time.sleep(1)
    running = any(t.is_alive() for t in threads)
    except KeyboardInterrupt:
    running = False
    Lie Ryan, Dec 11, 2009
    #4
  5. En Thu, 10 Dec 2009 20:43:48 -0300, mattia <> escribió:
    > Il Thu, 10 Dec 2009 23:10:02 +0000, Matthew Barnett ha scritto:
    >
    >> Only the main thread can receive the keyboard interrupt.

    >
    > Ok, so is there any way to stop all the threads if the keyboard interrupt
    > is received?


    If all other threads (except the main one) are "daemon" threads, then the
    whole process finishes when the main thread exits; all daemon threads are
    abruptly finished. Note that "try/finally" blocks are not honored, neither
    are "with" statements...

    If you want an orderly retreat, the other threads must cooperate (e.g. the
    main thread sets a flag and they periodically check for it).

    --
    Gabriel Genellina
    Gabriel Genellina, Dec 11, 2009
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steve Holden

    ASP KeyboardInterrupt errors

    Steve Holden, Jun 24, 2003, in forum: Python
    Replies:
    2
    Views:
    717
    Mark Hammond
    Jun 28, 2003
  2. Ivan Nestlerode

    KeyboardInterrupt and threading

    Ivan Nestlerode, Jan 2, 2004, in forum: Python
    Replies:
    8
    Views:
    516
    Anand Pillai
    Jan 8, 2004
  3. PantherSE
    Replies:
    0
    Views:
    333
    PantherSE
    May 16, 2005
  4. Operation Latte Thunder

    KeyboardInterrupt being lost?

    Operation Latte Thunder, Oct 14, 2005, in forum: Python
    Replies:
    1
    Views:
    397
    David Wahler
    Oct 14, 2005
  5. Tamas Nepusz
    Replies:
    4
    Views:
    620
    Dieter Maurer
    Oct 22, 2005
Loading...

Share This Page