473,396 Members | 1,917 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Making a timebomb

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

Aug 5 '05 #1
7 2928
ca********@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 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
Aug 5 '05 #2
ca********@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/
Aug 5 '05 #3
ca********@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
Aug 5 '05 #4
Cantankerous Old Git wrote:
ca********@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
Aug 6 '05 #5
Peter Hansen wrote:
Cantankerous Old Git wrote:
ca********@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.

I assume you know that I actually meant System.exit(). Why do you
think that won't help?
Aug 7 '05 #6
Cantankerous Old Git wrote:
Peter Hansen wrote:
Cantankerous Old Git wrote:
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.


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
Aug 7 '05 #7
Peter Hansen wrote:
Cantankerous Old Git wrote:
Peter Hansen wrote:
Cantankerous Old Git wrote:

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.

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


<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.
Aug 8 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Stewart | last post by:
Originally posted in comp.lang.javascript: Newsgroups: comp.lang.javascript From: "Stewart" Date: 23 Aug 2005 02:50:04 -0700 Local: Tues, Aug 23 2005 10:50 am Subject: FireFox, RemoveChild,...
7
by: redneon | last post by:
Does anyone have any good links to information on how it's possible to make a library in C++? I can't seem to find anything.
3
by: larry mckay | last post by:
Does anyone know how to develop or code a timebomb in vb.net that uses either the registry or something else that would be take a fair amount of time to hack. I'm developing an application that...
90
by: Ben Finney | last post by:
Howdy all, How can a (user-defined) class ensure that its instances are immutable, like an int or a tuple, without inheriting from those types? What caveats should be observed in making...
34
by: Asfand Yar Qazi | last post by:
Hi, I'm creating a library where several classes are intertwined rather tightly. I'm thinking of making them all use pimpls, so that these circular dependancies can be avoided easily, and I'm...
351
by: CBFalconer | last post by:
We often find hidden, and totally unnecessary, assumptions being made in code. The following leans heavily on one particular example, which happens to be in C. However similar things can (and...
10
by: JurgenvonOerthel | last post by:
Consider the classes Base, Derived1 and Derived2. Both Derived1 and Derived2 derive publicly from Base. Given a 'const Base &input' I want to initialize a 'const Derived1 &output'. If the...
7
by: MarkNeumann | last post by:
I'm coming from a Corel paradox background and moving into an Access environment. So I'm struggling with something that I think is probably way simpler than I'm making it out to be. Access 2007...
50
by: Juha Nieminen | last post by:
I asked a long time ago in this group how to make a smart pointer which works with incomplete types. I got this answer (only relevant parts included): ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.