exit entire process in threaded script

Peter Hansen peter at engcorp.com
Thu Nov 6 09:18:17 EST 2003


Hank wrote:
> 
> I have the following:
> 
> def readuser():
>    ...check for user input then exit entire script
> 
> thread.start_new_thread(readuser,())
> 
> while(forever):
>    ...do something
> 
> How can I exit the whole script from readuser()? sys.exit() only exits
> that thread and not the entire process. I'm trying not to use any
> win32 modules.

Since I don't see any other replies yet I'll give it a go.

The simplest thing to do is to make sure that the thread
that is doing the sys.exit() is the main thread (i.e. the
one which starts up in the first place, and which launches
all the other threads).  Then, make the other threads 
daemon threads with Thread.set_daemon() (sp?) and when the 
main thread exits via sys.exit(), the other threads are all
terminated.

-Peter




More information about the Python-list mailing list