Making a timebomb

Cantankerous Old Git CantankerousOldGit at gmail.com
Fri Aug 5 19:38:16 EDT 2005


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.  I was thinking of having a timebomb
> thread that starts when the server starts.  The timebomb sits, and
> sleeps for the specified timeout period (e.g., 5 hours), then does
> something to make the main thread terminate.  But I'm too inexperienced
> to figure out what that thing is.
> 
> Any suggestions?
> 
> 
> 
> class TimeBomb( threading.Thread ):
>    def run(self):
>        timeout = 5 * 60 * 60 #-- 3 hours
>        time.sleep( timeout )
>        MakeTheRestOfTheStuffDie()
> 
> class MyServer:
>    def __init__(self):
>        TimeBomb().run()
>        serve()
> 


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().

The cog



More information about the Python-list mailing list