SimpleXMLRPCServer daemon

T

Thomas Allen

I have a script that runs an instance of SimpleXMLRPCServer and in
general it works as expected. In its __del__, it is supposed to clean
up its PID file (written on boot). I have two problems with this
server instance: The first is that tt doesn't always clean up its PID
file; is there a more reliable way to do this than how I am currently?
The second is that when it does crash, I don't know about it...what
would be sufficient as a "keep-alive" script to restart it? I suppose
I could use something like EventMachine (already installed on my
server) to watch the PID file if it were deleted reliably.

Thomas Allen
 
A

Adam Tauno Williams

I have a script that runs an instance of SimpleXMLRPCServer and in
general it works as expected. In its __del__, it is supposed to clean
up its PID file (written on boot). I have two problems with this
server instance: The first is that tt doesn't always clean up its PID
file; is there a more reliable way to do this than how I am currently?
The second is that when it does crash, I don't know about it...what
would be sufficient as a "keep-alive" script to restart it? I suppose
I could use something like EventMachine (already installed on my
server) to watch the PID file if it were deleted reliably.

Why don't you wrap the server in a try/except block? Then if an
exception occurs killing the server you can automatically restart and/or
send an e-mail to an admin address.
 
T

Tim Wintle

The second is that when it does crash, I don't know about it...what
would be sufficient as a "keep-alive" script to restart it? I suppose
I could use something like EventMachine (already installed on my
server) to watch the PID file if it were deleted reliably.

If the server crashes then it clearly won't get around to deleting it's
pid file.

The way I do it is to use os.kill (with signal 0 IIRC) to check if the
process is still alive when the script starts. If it's not then I delete
the pid file and carry on starting up.

Tim
 
S

Sean DiZazzo

I have a script that runs an instance of SimpleXMLRPCServer and in
general it works as expected. In its __del__, it is supposed to clean
up its PID file (written on boot). I have two problems with this
server instance: The first is that tt doesn't always clean up its PID
file; is there a more reliable way to do this than how I am currently?
The second is that when it does crash, I don't know about it...what
would be sufficient as a "keep-alive" script to restart it? I suppose
I could use something like EventMachine (already installed on my
server) to watch the PID file if it were deleted reliably.

Thomas Allen

You should check out python-daemon. I use home grown daemons all the
time, and have only played a little with the python-daemon library,
but it is surely written well, and handles lockfiles and pidfiles.

I believe you can map a function to any signal that the daemon
receives to do any kind of cleanup etc. Ssometimes those PID files
might be left around, but the runner included with the module (i
think) checks for stale pid files.

There's all kinds of useful stuff.

In my home grown daemons I use Adam's technique, and wrap the "while
1:" block in a try except clause.

~Sean
 
G

Gabriel Genellina

I have a script that runs an instance of SimpleXMLRPCServer and in
general it works as expected. In its __del__, it is supposed to clean
up its PID file (written on boot). I have two problems with this
server instance: The first is that tt doesn't always clean up its PID
file; is there a more reliable way to do this than how I am currently?
The second is that when it does crash, I don't know about it...what
would be sufficient as a "keep-alive" script to restart it? I suppose
I could use something like EventMachine (already installed on my
server) to watch the PID file if it were deleted reliably.

I agree with the recommendation of using some daemon library; doing it
right is better left to the experts :)
But if you can't or don't want to alter your code so much, I suggest:

- use atexit.register, instead of __del__, to delete the PID file
- keep a lock on the pid file; if a second instance is able to write to
it, it means it's an orphan from a previous, aborted run. (Checking
whether a process with such pid exists is not enough - pids are recycled
rather fast)
 
J

Jean-Michel Pichavant

Gabriel said:
En Fri, 29 Jan 2010 12:54:23 -0300, Thomas Allen


I agree with the recommendation of using some daemon library; doing it
right is better left to the experts :)
But if you can't or don't want to alter your code so much, I suggest:

- use atexit.register, instead of __del__, to delete the PID file
- keep a lock on the pid file; if a second instance is able to write
to it, it means it's an orphan from a previous, aborted run. (Checking
whether a process with such pid exists is not enough - pids are
recycled rather fast)
see also http://code.activestate.com/recipes/278731/

JM
 

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
473,774
Messages
2,569,596
Members
45,140
Latest member
SweetcalmCBDreview
Top