8

I was trying to understand how non-blocking sockets work ,so I wrote this simple server in python .

import socket

s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)

s.bind(('127.0.0.1',1000))
s.listen(5)
s.setblocking(0)
while True:
      try:
            conn, addr = s.accept()
            print ('connection from',addr)
            data=conn.recv(100)
            print ('recived: ',data,len(data))

      except:
          pass  

Then I tried to connect to this server from multiple instances of this client

import socket

s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('127.0.0.1',1000))
while True:
    continue

But for some reason putting blocking to 0 or 1 dose not seem to have an effect and server's recv method always block the execution. So, dose creating non-blocking socket in python require more than just setting the blocking flag to 0.

Gray
  • 267
  • 1
  • 6
  • 12

3 Answers3

13

setblocking only affects the socket you use it on. So you have to add conn.setblocking(0) to see an effect: The recv will then return immediately if there is no data available.

Phillip
  • 13,448
  • 29
  • 41
  • `setblocking(0)` _does_ affect `socket.accept()` which will _not_ block waiting for an incoming connection, but raise an exception. – mhawke Aug 21 '14 at 12:51
  • The documentation seems to be incomplete here, you are of course right. I've removed that comment from my answer. But there is an exception to this: If `socket.settimeout` is used, Python will still call `select(2)` before `accept(2)`, i.e. wait at least until the timeout is reached. – Phillip Aug 21 '14 at 13:26
3

You just need to call setblocking(0) on the connected socket, i.e. conn.

import socket

s = socket.socket()
s.bind(('127.0.0.1', 12345))
s.listen(5)
s.setblocking(0)
>>> conn, addr = s.accept()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python2.7/socket.py", line 202, in accept
    sock, addr = self._sock.accept()
socket.error: [Errno 11] Resource temporarily unavailable

# start your client...
>>> conn, addr = s.accept()
>>> conn.recv()            # this will hang until the client sends some data....
'hi there\n'
>>> conn.setblocking(0)    # set non-blocking on the connected socket "conn"
>>> conn.recv()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
socket.error: [Errno 11] Resource temporarily unavailable
mhawke
  • 84,695
  • 9
  • 117
  • 138
-1

https://docs.python.org/3/library/socket.html#socket.setdefaulttimeout

You can use s.setdefaulttimeout(1.0) to apply all connection sockets as default.

Peyman Mohamadpour
  • 17,954
  • 24
  • 89
  • 100