Why sys.exit does not exit ?

Peter Hansen peter at engcorp.com
Thu Mar 13 10:42:27 EST 2003


Giorge wrote:
> 
> Hi all, here is a small code snippet that should
> tell something about what I'm trying to do:
> 
> import sys, os, thread, time
> from threading import *
> 
> def quit ():
>     print "10 seconds passed. We should exit right now !"
>     sys.exit(0)
> 
> # main program:
> t = Timer (10.0, quit )
> t.start()
> sleep(20)
> 
> The problem here is that the sys.exit call doesn't
> terminate the program, which exits after the sleep
> call.

In this case, the following loop should  cleanly terminate your
other threads which based on Timer:  

(warning: untested code)

t = Timer(10, quit)
t.start()
sleep(20)

import threading   # you should avoid the "from xxx import *" form!
for t in threading.enumerate():
    try:
        t.cancel()  # ask Timers to stop themselves
    except:
        pass

-Peter




More information about the Python-list mailing list