[Python-Dev] Non-blocking sockets, asynchronous connects and select.select. (original) (raw)

Neal Norwitz nnorwitz at gmail.com
Tue Mar 20 06:36:46 CET 2007


Hi Alan.

Are you running on Windows or Unix? I just tested 2.4 - 2.6 on Linux and all report:

Server socket: accept would not block Client socket: write would not block

Which seems to be what you would expect unless I read it wrong.

I vaguely remember some issue about an empty hostname on Windows.

n

On 3/19/07, Alan Kennedy <python-dev at alan.kennedy.name> wrote:

Dear all,

I'm working on a select implementation for jython (in combination with non-blocking sockets), and a set of unit tests for same. I decided to run all of the non-blocking unit tests, both server and client side, in the same thread; seems to be a reasonable thing to do. After all, avoiding threads is one of the great benefits of non-blocking sockets. Also, I'm writing the tests so that they'll hopefully pass on both cpython and jython. But I'm getting behaviour I don't expect on cpython, when a non-blocking accept and connectex is used, for the server and client sides respectively. The following piece of code outputs what I expect on my jython implementation, which is that both the server accept and client write calls would NOT block. But when I run the code on cpython, the code reports that both calls would block, i.e. that neither side of the socket will progress with the connection. The code is # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- import socket import select SERVERADDRESS = ("", 54321) serversocket = socket.socket(socket.AFINET, socket.SOCKSTREAM) serversocket.setblocking(0) serversocket.bind(SERVERADDRESS) serversocket.listen(5) clientsocket = socket.socket(socket.AFINET, socket.SOCKSTREAM) clientsocket.setblocking(0) result = clientsocket.connectex(SERVERADDRESS) rfds, wfds, xfds = select.select([serversocket], [clientsocket], [], 1) if serversocket in rfds: print "Server socket: accept would not block" else: print "Server socket: accept would block" if clientsocket in wfds: print "Client socket: write would not block" else: print "Client socket: write would block" serversocket.close() clientsocket.close() # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- Is there some call that I am missing, e.g. something along the lines of the java finishConnect() method on SocketChannel? http://java.sun.com/j2se/1.4.2/docs/api/java/nio/channels/SocketChannel.html#finishConnect() Is there any way to make the above code report, on cpython, that neither side of the socket would block? Am I missing some essential method call that would make either side progress to a connected socket? Thanks, Alan.


Python-Dev mailing list Python-Dev at python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/nnorwitz%40gmail.com



More information about the Python-Dev mailing list