Making a timebomb

Benjamin Niemann pink at odahoda.de
Fri Aug 5 19:57:54 CEST 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()

Unfortunately you can raise an exception in another thread. You could tell
tell main thread to terminate by setting a flag that is polled by the main
thread.
You could also try to send a signal to yourself, but I'm not sure what will
happen then...


-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/



More information about the Python-list mailing list