Python Forum
Clarification on how to set up non-blocking socket connection
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Clarification on how to set up non-blocking socket connection
#1
Hi

-------------------------------------------------
System Information
Red Hat Enterprise Linux Server release 6.6 (Santiago)
Linux watvde0453 2.6.32-504.30.3.el6.x86_64 #1 SMP Thu Jul 9 15:20:47 EDT 2015 x86_64 x86_64 x86_64 GNU/Linux

CentOS release 6.8 (Final)
Linux lb-cam-bca-fsbackup 2.6.32-642.el6.x86_64 #1 SMP Tue May 10 17:27:01 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

CentOS Linux release 7.6.1810 (Core)
Linux lb-cbga-eq 3.10.0-1062.1.1.el7.x86_64 #1 SMP Fri Sep 13 22:55:44 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

# python version 2
>>> sys.version
'2.7.5 (default, Dec 10 2013, 00:34:00) \n[GCC 4.4.6 20120305 (Red Hat 4.4.6-4)]'

# python version 3
>>> sys.version
('3.7.1 (default, Dec 7 2018, 02:32:23) \n'
'[GCC 4.4.6 20120305 (Red Hat 4.4.6-4)]')
-------------------------------------------------

Could someone clarify how a non-blocking socket connection is established, and also how to determine whether a socket is in blocking or non-blocking mode ?

From the documentation I see there is a setblocking() method that allows you to set the blocking mode.
However I am unclear where this needs to be set.

To use non-blocking sockets do I have to set the blocking mode to 0 on the (server side) listening socket, the client side connecting socket and the server side connecting socket as below:
# server side
# server side listener
listens=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
listens.bind(('127.0.0.1',1000))
listens.listen(5)
listens.setblocking(0) # set non blocking here

# server side accepting connection
while 1:
   ...
   conn, addr = s.accept()
   conn.setblocking(0) # set non blocking here
   ...


# client side
...
clientSocket = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
clientSocket.connect ((serverhost, serverport))
clientSocket.setblocking(0) # set non blocking here
...
Or do I need to set non-blocking on only a sub-set of these sockets ?

If all sockets involved need to have non-blocking mode explicitly set, what happens if one or end of the connection has blocking mode = 1 and the other end blocking mode = 0 ?

Once a connection is established is there anyway of determining whether the socket is in blocking or non-blocking mode from the socket object itself ?

Are there any differences in socket non-blocking configuration/behaviour between python 2.x and python 3.x ?
Reply
#2
you can set or unset the blocking mode on either socket any time you want.  blocking mode is just the way read/write/receive/send will work on it.  just have the right mode when you do a method on it.  it is good practice to use only one mode and set it that way as soon as you can.  do you need to switch between the two modes?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  socket without blocking loop and automatically retrieve data from the port RubenP 3 3,487 Jun-21-2020, 10:59 PM
Last Post: Gribouillis
  Using BaseHTTPServer to encounter system blocking zzs027 0 2,678 Mar-05-2020, 01:39 PM
Last Post: zzs027
  Multi connection socket server help! MuntyScruntfundle 0 2,685 Feb-19-2019, 12:03 PM
Last Post: MuntyScruntfundle
  Python // C # - SOCKET connection is constantly interrupted raspberryPiBRA 0 2,386 Feb-01-2018, 09:53 AM
Last Post: raspberryPiBRA

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020