How to Force exiting from program/script

Discussion in 'Python' started by Alex, Jul 15, 2009.

  1. Alex

    Alex Guest

    hi at all,
    I have made a script with a while loop and I want that after 30
    seconds the program stop and exit . But the code like this doesn't
    run:
    In the Console I can see work so that function is correctly called...

    #Function to exit
    def exit():
    print "work"
    raise SystemExit()
    t = threading.Timer(30.0, exit)
    t.start()

    # Loop
    while True:
    ....many lines....
    Alex, Jul 15, 2009
    #1
    1. Advertising

  2. Alex schrieb:
    > hi at all,
    > I have made a script with a while loop and I want that after 30
    > seconds the program stop and exit . But the code like this doesn't
    > run:
    > In the Console I can see work so that function is correctly called...
    >
    > #Function to exit
    > def exit():
    > print "work"
    > raise SystemExit()
    > t = threading.Timer(30.0, exit)
    > t.start()
    >
    > # Loop
    > while True:
    > ...many lines....
    >


    This works for me:

    import threading
    import time

    def work():
    while True:
    pass



    t = threading.Thread(target=work)
    t.setDaemon(True)
    t.start()

    time.sleep(10)
    raise SystemExit


    The trick is to raise the SystemExit in the main-thread.

    Diez
    Diez B. Roggisch, Jul 15, 2009
    #2
    1. Advertising

  3. >>>>> Alex <> (A) a écrit:

    >A> hi at all,
    >A> I have made a script with a while loop and I want that after 30
    >A> seconds the program stop and exit . But the code like this doesn't
    >A> run:
    >A> In the Console I can see work so that function is correctly called...


    >A> #Function to exit
    >A> def exit():
    >A> print "work"
    >A> raise SystemExit()
    >A> t = threading.Timer(30.0, exit)
    >A> t.start()


    >A> # Loop
    >A> while True:
    >A> ...many lines....


    This code gives you a bit more control as it doesn't just force a system
    exit, but allows you to continue some other work after aborting the
    loop:

    import signal, os
    from threading import Timer

    signalcode = signal.SIGALRM
    class AlarmError(Exception):
    pass

    def handler(signum, frame):
    raise AlarmError, "command lasts too long"

    signal.signal(signalcode, handler)

    def interrupt():
    os.kill(os.getpid(), signalcode)

    def execute(function, timeout):
    Timer(timeout, interrupt).start()
    try:
    function()
    except AlarmError, e:
    print e
    print 'Execution aborted'

    def long_function():
    while True:
    pass

    print "The everlasting command"
    execute(long_function, 10)
    print "The End"

    --
    Piet van Oostrum <>
    URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
    Private email:
    Piet van Oostrum, Jul 16, 2009
    #3
    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. Abby
    Replies:
    1
    Views:
    487
    Jack Klein
    Aug 29, 2003
  2. knguyen
    Replies:
    9
    Views:
    2,810
    Roedy Green
    Feb 15, 2008
  3. W. eWatson
    Replies:
    0
    Views:
    217
    W. eWatson
    Feb 5, 2010
  4. Alex Hall

    exiting threaded program?

    Alex Hall, Mar 24, 2010, in forum: Python
    Replies:
    1
    Views:
    274
    Alf P. Steinbach
    Mar 24, 2010
  5. Pengcheng Chen

    Callbacks on program exiting/aborting

    Pengcheng Chen, Mar 3, 2011, in forum: Python
    Replies:
    1
    Views:
    200
    Grant Edwards
    Mar 3, 2011
Loading...

Share This Page