select.select and socket.setblocking

Laszlo Nagy gandalf at shopzeus.com
Tue Dec 30 14:42:32 CET 2008


I'm using this method to read from a socket:

    def read_data(self,size):
        """Read data from connection until a given size."""
        res = ""
        fd = self.socket.fileno()
        while not self.stop_requested.isSet():
            remaining = size - len(res)
            if remaining<=0:
                break
            # Give one second for an incoming connection so we can stop the
            # server in seconds when needed
            ready = select.select([fd], [], [], 1)
            if fd in ready[0]:
                data = self.socket.recv(min(remaining,8192)) # 8192 is
recommended by socket.socket manual.
                if not data:
                    # select returns the fd but there is no data to read
-> connection closed!
                    raise TransportError("Connection closed.")
                else:
                    res += data
            else:
                pass
        if self.stop_requested.isSet():
            raise SystemExit(0)
        return res


This works: if I close the socket on the other side, then I see this in
the traceback:

  File "/usr/home/gandalf/Python/Projects/OrbToy/orb/endpoint.py", line
233, in read_data
    raise TransportError("Connection closed.")
TransportError: Connection closed.

Also when I call stop_requested.set() then the thread stops within one
seconds.

Then I switch to non blocking mode, my code works exactly the same way,
or at least I see no difference.

I have read the socket programming howto (
http://docs.python.org/howto/sockets.html#sockets ) but it does not
explain how a blocking socket + select is different from a non blocking
socket + select. Is there any difference?

Thanks




More information about the Python-list mailing list