Bottom Page

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
Quote
#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.
Quote
#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 
    
Quote
#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.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Using BaseHTTPServer to encounter system blocking zzs027 0 1,231 Mar-05-2020, 01:39 PM
Last Post: zzs027
  TCP socket, data transnission Simba 4 589 Dec-07-2019, 03:15 PM
Last Post: Simba
  socket loop problem monamour 9 804 Nov-28-2019, 12:04 PM
Last Post: buran
  Soft Access Point & Socket Data Streaming groger57 1 486 Aug-01-2019, 02:53 PM
Last Post: groger57
  Send data BMP180 between client and server trought module socket smalhao 0 1,002 Jul-30-2018, 12:56 PM
Last Post: smalhao
  Python socket : Error receive data quanglnh1993 1 5,997 Mar-14-2018, 11:59 AM
Last Post: avorane
  Socket won't receive data suddenly with multiple clients SquareRoot 0 1,317 Sep-06-2017, 09:09 PM
Last Post: SquareRoot
  socket server with SSL accepting unencrypted data kopite 0 1,498 Apr-20-2017, 08:31 AM
Last Post: kopite

Forum Jump:


Users browsing this thread: 1 Guest(s)