Sockets and time-outs

Donn Cave donn at u.washington.edu
Mon Aug 14 12:10:27 EDT 2000


Quoth Ulf Engstrøm <ulf.engstrom at b2b-link.com>:
| I'm using the socket module for a program, but there's one thing that I want
| to improve on it.
| The client side of this sends data to the server and always get a reply
| back, but sometimes the server sends something that wasn't requested of the
| reply comes in two sends. Thus I need to read the socket sometimes to see if
| there's something waiting. Using recv() is great for getting a reply I know
| is there, but when using it and there's no message there it takes an awfully
| long time to time out and waiting for this time-out blocks the sending on
| the same socket. Is there any way to set a shorter time-out (basically just
| check for 2 secs or so) or is there an other function for this?

I would use select, like this for a very simple example:

    import select
    ...
    input = [srv_con]
    input, output, error = select.select(input, [], [], 2.0)
    if srv_con in input:
        srv_input = srv_con.recv(8192)

Now depending on your protocol, it may be convenient to put this select
in a very central place in the system.  Protocols like that, where the
dialogue doesn't simply trade back and forth, are somewhat more challenging.

	Donn Cave, donn at u.washington.edu



More information about the Python-list mailing list