Python Forum
socket without blocking loop and automatically retrieve data from the port
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
socket without blocking loop and automatically retrieve data from the port
#1
I have a program in a loop.
how to add data transmission via socket in a loop without blocking the loop itself.
and automatically retrieve data from the port if there is data transmission from the client (maybe an event-driven concept)

code :
bind_port = 1234
# create as a server

def myloop ():
    global dataRX
    loopx=1
    while loopx < 10000:
          dataTX = process (DataRX)
          sock.send(dataTX) #send dataTX via port 
          loopx = loopx + 1   


def receive ():
#automatically retrieve data from client
       DataRX = sock.recv() # get data from client

process (param):
       ... code process ...
Thank You
Reply
#2
Sockets have a setblocking(bool) method that can be used to prevent a loop from blocking. An example of event driven program for a socket server is shown in the documentation of the selectors module. You could probably use this code.
Reply
#3
I use this program code.
the disadvantage of this code is program speed decreases and loop into one part with the socket program.

Is there a solution so that the loop is separate from the socket program like other programming events such as "OnReceive", "OnConnect"

code :
    service = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    service.bind(("", PORT))
    service.listen(1)
    global datarx
    print ("listening on port", PORT)
    datarx=""
    process = 1
    while 1:
        is_readable = [service]
        is_writable = []
        is_error = []
        
        r, w, e = select.select(is_readable, is_writable, is_error, 1.0)
        if r :
            channel, addr = service.accept()      
            request = channel.recv(1024)    
            datarx=request.decode()
            channel.send(request)
            print('request ', request)
            channel.close() # disconnect
            
        else:
            # my program loop
            if continue == 1 :
               --- process 
                 if datarx = '1' :
                    -- process 1
                 else :
                     --- process 2 
                 if process == complete :
                       continue = 0 
    
Reply
#4
A solution that I have used in the pass is to create a socketserver.ThreadingTCPServer instance and launch a thread that runs the server's serve_forever() method. One needs to create a subclass of socketserver.StreamRequestHandler with a handle() method that does something useful.Once the server is launched, the main thread can do anything it wants and the socketserver will handle connections and requests in other threads as they arrive.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Socket to stream data and tranmit and revieve data kiyoshi7 0 2,369 Aug-11-2022, 10:52 PM
Last Post: kiyoshi7
  how to split socket data onto multiple clients for separate processing ConcealedFox70 0 1,939 Jan-11-2022, 08:26 PM
Last Post: ConcealedFox70
  Help with socket library for a port scanner? xanderv 1 2,326 Jan-03-2021, 10:48 PM
Last Post: Larz60+
  Clarification on how to set up non-blocking socket connection Shaggy 1 1,976 Oct-14-2020, 07:57 PM
Last Post: Skaperen
  Using BaseHTTPServer to encounter system blocking zzs027 0 2,744 Mar-05-2020, 01:39 PM
Last Post: zzs027
  TCP socket, data transnission Simba 4 3,278 Dec-07-2019, 03:15 PM
Last Post: Simba
  socket loop problem monamour 9 6,678 Nov-28-2019, 12:04 PM
Last Post: buran
  Soft Access Point & Socket Data Streaming groger57 1 2,521 Aug-01-2019, 02:53 PM
Last Post: groger57
  Send data BMP180 between client and server trought module socket smalhao 0 2,839 Jul-30-2018, 12:56 PM
Last Post: smalhao
  Python socket : Error receive data quanglnh1993 1 13,019 Mar-14-2018, 11:59 AM
Last Post: avorane

Forum Jump:

User Panel Messages

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