473,569 Members | 2,457 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Threa d ):
def run(self):
timeout = 5 * 60 * 60 #-- 3 hours
time.sleep( timeout )
MakeTheRestOfTh eStuffDie()

class MyServer:
def __init__(self):
TimeBomb().run( )
serve()

Aug 5 '05 #1
7 2947
ca********@gmai l.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********@gmai l.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.Threa d ):
def run(self):
timeout = 5 * 60 * 60 #-- 3 hours
time.sleep( timeout )
MakeTheRestOfTh eStuffDie()

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********@gmai l.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.Threa d ):
def run(self):
timeout = 5 * 60 * 60 #-- 3 hours
time.sleep( timeout )
MakeTheRestOfTh eStuffDie()

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********@gmai l.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********@gmai l.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.Syste m.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
1450
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, AppendChild, making width grow? Hi,
7
3117
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
3086
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 I'm trying to prevent users from distributing it after a particular time expires and I do not want for it to be obvious for them to detect the...
90
4344
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 immutable instances? -- \ "Love is the triumph of imagination over intelligence." -- |
34
3676
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 thinking of making all these pimpl class declarations public. Reasoning is that since only the code within the ..cc file will need to ever access...
351
12839
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 do) occur in any language. These assumptions are generally made because of familiarity with the language. As a non-code example, consider the idea...
10
3416
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 dynamic type of 'input' is Derived1, then 'output' should become a reference to 'input'. Otherwise 'output' should become a reference to the (temporary)...
7
2300
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 WindowsXP SP2 What I have is a table in a subform that tracks dollars spent per project There is sub sub form that breaks down each dollar amount...
50
4436
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): //------------------------------------------------------------------ template<typename Data_t> class SmartPointer { Data_t* data; void(*deleterFunc)(Data_t*);
0
7703
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7926
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8132
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7678
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6286
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5222
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2116
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1226
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.