[Python-Dev] Non-blocking sockets, asynchronous connects and select.select.

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 connect_ex 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
>
> SERVER_ADDRESS = ("", 54321)
>
> server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> server_socket.setblocking(0)
> server_socket.bind(SERVER_ADDRESS)
> server_socket.listen(5)
>
> client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> client_socket.setblocking(0)
>
> result = client_socket.connect_ex(SERVER_ADDRESS)
>
> rfds, wfds, xfds = select.select([server_socket], [client_socket], [], 1)
> if server_socket in rfds:
>     print "Server socket: accept would not block"
> else:
>     print "Server socket: accept would block"
> if client_socket in wfds:
>     print "Client socket: write would not block"
> else:
>     print "Client socket: write would block"
>
> server_socket.close()
> client_socket.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