Making a timebomb
Peter Hansen
peter at engcorp.com
Fri Aug 5 22:10:49 EDT 2005
Cantankerous Old Git wrote:
> callmebill at gmail.com wrote:
>
>> I have a server that right now runs infinitely. I'd like to make it
>> die after some amount of time.
>
> The proper way to do it is to have the timer set a flag that the other
> threads check regularly. The threads can then clean up and exit asap.
>
> The dirty way, which can leave corrupt half-written files and other
> nasties, is something like sys.exit().
sys.exit() won't help you if your server is running in the main thread,
nor if your server thread is not marked as a daemon, but that does raise
another possibility. Instead of doing serve() in the main thread, spawn
off a child thread to do the serving, and call setDaemon(True) on it.
Then the _main_ thread can do sys.exit() and the server thread will be
terminated (somewhat messily perhaps) -- even if it is blocked in an
accept() call or some other external blocking call.
-Peter
More information about the Python-list
mailing list