Making a timebomb

C

callmebill

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

Peter Hansen

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 MyServer:
def __init__(self):
serve()

The right answer depends entirely on things you don't mention, most
specifically just what serve() is doing in the above code.

Generally speaking, long-running code is either running in a loop,
repeating some operations -- and a polling technique (to check for
timeout) can be used -- or it is asynchronous, such as a server
listening on sockets -- in which case you need either to use whatever
builtin support is in the framework you're building on (and you should
be building on a framework, not doing this from scratch), or you need to
use non-blocking sockets and select(), or in the worst case you need to
have your server connect internally to itself (think "loopback") and
send itself a message so that the blocked threads will wake up and can
then voluntarily terminate.

Not the clearest message I've written lately... sorry, but if that's not
enough to point you in the right direction, give more detail and you'll
get a better answer.

-Peter
 
B

Benjamin Niemann

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...
 
C

Cantankerous Old Git

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
 
P

Peter Hansen

Cantankerous said:
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
 
P

Peter Hansen

Cantankerous said:
I assume you know that I actually meant System.exit(). Why do you think
that won't help?

No, I didn't know that, but if you were confused the first time, I think
you're getting even more confused now. What is System.exit()? I
don't have one, and have never seen it mentioned. Perhaps you meant
SystemExit, the exception that's raised when you call sys.exit()? If
so, I still don't see your point, because there's no difference between
the two in this context.

Maybe you meant os._exit()? Now *that* one is messy, and will work as
you described.

-Peter
 
C

Cantankerous Old Git

Peter said:
No, I didn't know that, but if you were confused the first time, I think
you're getting even more confused now. What is System.exit()? I don't
have one, and have never seen it mentioned. Perhaps you meant
SystemExit, the exception that's raised when you call sys.exit()? If
so, I still don't see your point, because there's no difference between
the two in this context.

Maybe you meant os._exit()? Now *that* one is messy, and will work as
you described.

-Peter

<TILT><RESET>

Yup - I guess you're not interested in java.lang.System.exit() at
all, are you. You're right about me getting confused!

Perhaps I should take a break between reading the two newsgroups.
Doh!

sys.exit() docs (for Python) say it raises a SystemExit
exception. A quick test shows that:
* You can catch this to prevent being killed
* It only gets raised on the calling thread - not the others

So you're right - sys.exit is not very helpful in this case.

os._exit is the one I was thinking of - equivalent to java's
System.exit().

And to the OP, Bill - sorry for messing you around. As you see -
I got confused.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,262
Messages
2,571,311
Members
47,985
Latest member
kazewi

Latest Threads

Top