'Threads hang when flask application is opened in several devices/tabs/browsers

I'm in the middle of a project that uses peer-to-peer and server-client architecture. I have a server that communicates with another device through sockets (I have three sockets for both server and device). Each socket is responsible for a certain type of information. I also need the tasks associated with each socket to work in parallel.

Let me explain further: I have a device (GPU) that is processing images. This GPU will need to send to server three different things in parallel. So I'm using three threads and three sockets. On the server side I have three threads("listeners") that will need to connect to the three GPU "clients". After there are three connections, three more threads are open one for each socket and task. Basically the gpu tries to connect to the server (3 sockets) and when the connections are made, each listener creates another thread for the task. When I open the Flask application everything works fine, the info is driven from server to browser without problems. But when I open the app on another tab or browser or computer, the server part hangs. At this point only one task is working on server side. I would like to give you some code, but this is company property so I can't.

I'll give you a pseudo-code.

if __name__=="__main__"
    threading.Thread(target=app.listen1).start()
    app.run(threaded)

def listen1():
     with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s1, socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s2, socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s3:
       s1.bind((HOST, PORT))
       s1.listen()
       s2.bind((HOST, PORT2))
       s2.listen()
       s3.bind((HOST, PORT3))
       s3.listen()
       while True:
          conn1, addr1 = s1.accept()
          conn2, addr2 = s2.accept()
          conn3, addr3 = s3.accept()
          ident = json.loads(conn1.recv(1024))
          if db.existStream(ident['id'])==True:
             conn1.send(b'True')
             threading.Thread(target=client1, args=(conn1, addr1)).start()
             conn2.send(b'True')
             threading.Thread(target=client2, args=(conn2, addr2)).start()
             conn3.send(b'True')
             threading.Thread(target=client3, args=(conn3, addr3)).start()   
          else:
             conn1.send(b'False')
             conn2.send(b'False')
             conn3.send(b'False')
          
def client1(conn, addr):
     buffer1=b''
     while True:
        length= int.from_bytes(conn.recv(1024), 'big')
        if length==0:
           break;
        conn.send(b"ACK")
        while len(buffer1)<length:        
            data = conn.recv(262144)
            buffer1 += data
        buffer2=json.loads(buffer1.decode('utf-8'))
        overlay(buffer2['frame'], buffer2['sectorX'])
        if 'confidence' in buffer2:
           db.guardaFrame(buffer2['frame'], buffer2['sectorX'], buffer2['sectorY'], buffer2['timestamp'], buffer2['azimuth'], buffer2['elevation'])  
           db.guardaAlerta(buffer2['frame'], buffer2['sectorX'], buffer2['sectorY'], buffer2['timestamp'], buffer2['azimuth'], buffer2['elevation'], buffer2['confidence'])
        else:
           db.guardaFrame(buffer2['frame'], buffer2['sectorX'], buffer2['sectorY'], buffer2['timestamp'], buffer2['azimuth'], buffer2['elevation'])  
        buffer1=b''
          
def client2(conn, addr):
     buffer1=b''
     while True:
        length= int.from_bytes(conn.recv(1024), 'big')
        if length==0:
           break;
        conn.send(b"ACK")
        while len(buffer1)<length:        
            data = conn.recv(262144)
            buffer1 += data
            
        global Frame
        Frame= b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' + buffer1 + b'\r\n'
        buffer1=b''
          
def client3(conn, addr):
     buffer1=b''
     while True:
        length= int.from_bytes(conn.recv(1024), 'big')
        if length==0:
           break;
        conn.send(b"ACK")
        while len(buffer1)<length:        
            data = conn.recv(1024)
            buffer1 += data
            
        buffer2=json.loads(buffer1.decode('utf-8'))
        global azimuth
        azimuth=buffer2['azimuth']

So, in conclusion, everything works fine when the user only opens a browser window. When the user opens on another computer (of the same network), on in another tab, the clients hang and don't do their tasks (except one). I hope this is enough for you to understand.

Thanks

EDIT: Forgot to mention, the code should be able to accept more equal devices. I mean I need to "listen" for more connections.

EDIT2: So I decided to show the code for better understanding. The "If ..." is on another file.



Solution 1:[1]

Wrapping your threading in a class is a bad idea, since the instantiation of the class will force it to be single threaded.

class Server:
  def __init__(self, port):
      self.port = port

  def client(self):
      task

  def listen(self):
      socket created
      while true:
         do stuff
         break
     return

if __name__=="__main__"
     ports = [123,124,125]
     threads = [for p in ports in threading.Thread(Server(p).listen)]
     threads.join()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 eatmeimadanish