How to Force exiting from program/script

Diez B. Roggisch deets at
Wed Jul 15 17:39:01 EDT 2009

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:

t = threading.Thread(target=work)

raise SystemExit

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


More information about the Python-list mailing list