need a thread to keep a socket connection alive?

Rene Pijlman reply.in.the.newsgroup at my.address.is.invalid
Sat Apr 22 12:23:41 EDT 2006


nephish at xit.net:
>i have a script that waits for message packets from a data server over
>a socket.

Using what network protocol?

>it works fine for a while, but the server requires that i send a
>heartbeat ping every 600 seconds or it will terminate the connection.
[...]
>should i do this with threads? i dont want to interrupt the listening
>cycle to send a ping.

If this is a TCP connection with a conversational protocol, you can't have
two threads reading bytes of the socket, without some sort of
coordination. When one thread parses the bytes it received, some bytes may
be part of the next message for the other thread.

You may be better off with asynchronous I/O and a state machine model.
http://squirl.nightmare.com/medusa/async_sockets.html
http://www.python.org/doc/lib/module-asyncore.html
http://twistedmatrix.com/projects/core/documentation/howto/clients.html

-- 
René Pijlman



More information about the Python-list mailing list