Checking whether an async* dispatcher is "alive"

Van Gale cgale1 at cox.net
Sat Apr 13 21:54:54 EDT 2002


"Magnus Lie Hetland" wrote:
> In a server I'm working on, I need to check whether an asynchat
> dispatcher is still connected, i.e. whether I can send it data. I've
> tried to use try/except (with socket.error) but since async* catches
> that itself, It's no good.
>
> Are there any methods/attributes that indicate whether I can send data
> on a dispatcher? (Possibly some method/attribute on its socket
> object?)

Hmm, this is a bit tricky and depends some on how your protocol works.

If your protocol is request/response oriented (primarily what
asyncore/asynchat were designed for), check out http_server.py and
status_handler.py in the medusa distribution (www.nightmare.com).  These
take a fairly simple approach: after every X number of connections (default
X = 500) close any sockets that have been open for longer than 30 minutes.
This works fine for request/response like HTTP where 30 minutes is probably
a good indicator it's a zombie.

I presume however, that your protocol is asynchronous over a persistent
connection since you are implying you want to send data to a client without
having just recieved a request.  In this case you really only have 2
choices: either just trust TCP's built-in reliability, tr-try, and
keep-alive features or you can implement some kind of heartbeat message.
See pg 581 of [1] for a discussion and sample C code for using TCP's ugent
mode to implement heartbeats.  His implementation has the client send the
heartbeats and the server raises a signal if it hasn't recieved any
heartbeats after a specified interval.  I don't know how portable this is,
or even how easy it would be to implement in Python if you're using async*
classes.

My personal preference (atm) is just trusting TCP and handling errors when
the connection is broken because you're going to get a send error on the
heartbeat anyway :)  If you're implementing something like a server for an
IM protocol (e.g. Jabber) which needs up to date "presence" information,
then it should have a heartbeat built-in to the protocol (as does Jabber).

Btw, the Medusa http_server, status_handler, and the monitor_server, are
probably worth checking out even if your protocol is fully asynchronous.  I
really like having a port I can connect to with a browser that gives me a
nice picture of the internal status of the running server, and
monitor_server takes the ultimate step: it gives you an interactive Python
session inside your running server.

Van
[1] W. Richard Stevens "Unix Network Programming: Networking APIs: Sockets
and XTI Volume 1"






More information about the Python-list mailing list