Non-blocking sockets, asynchronous connects and select.select.
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...) 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.
"Alan Kennedy" <python-dev@alan.kennedy.name> wrote:
I'm working on a select implementation for jython (in combination with non-blocking sockets), and a set of unit tests for same.
Great! [snip]
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. [snip] SERVER_ADDRESS = ("", 54321)
Replacing the above with: SERVER_ADDRESS = ("localhost", 54321) ...makes it work for me on Windows CPython 2.3.5 and 2.5 . - Josiah
Thanks Josiah and Neal, you were right. I am running the code on Windows, Server 2003. I should have mentioned that at the start. When I change the hostname from "" to "localhost", the code works exactly as expected. It's odd that this behaviour exists, only on Windows. The jython code that I'm writing worked fine with an empty hostname "". That jython code uses the java.net and java.nio APIs directly, and does not have any special cases for empty hostname. Which must mean that the underlying java implementations must be correctly handling the empty hostname, even when running on Windows. (Perhaps by special casing for the Windows implementation, if indeed a peculiarity of the Windows socket libraries is the cause). Which implies to me that cpython, like java, should not have different behaviour on Windows vs. other platforms; it lessens portability. Lack of portability is confirmed by Neal's report that the original unmodified snippet (i.e. with hostname == "") works correctly under cpython on Linux. Thanks guys, Alan. On 3/20/07, Josiah Carlson <jcarlson@uci.edu> wrote:
[snip]
SERVER_ADDRESS = ("", 54321)
Replacing the above with:
SERVER_ADDRESS = ("localhost", 54321)
...makes it work for me on Windows CPython 2.3.5 and 2.5 .
- Josiah
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@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...)
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@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/nnorwitz%40gmail.com
participants (3)
-
Alan Kennedy -
Josiah Carlson -
Neal Norwitz