title: Multithreaded multiple socket unexpected closed in Python
body: <p>I have a C++ server program running, and my Python client is trying to grab some data from it. I would like to spawn multiple clients to get the data simultaneously. The following are the codes for the data grabbing part and its initialization. The weird part is when the data grabbing is happening, some sockets would just die unexpectedly. As I understand it, the way the class being spawned have established eight independent connections. They should operate individually without interfering each other. Any idea what is closing down the socket?</p>
  
  <p>Data grabbing class:</p>
  
  <pre><code>class irp(Thread):
      def __init__(self, cfg, total_scan_pass):
          Thread.__init__(self)
          self.cfg = cfg
          self.total_scan_pass = total_scan_pass
          self.camera_ip = cfg.get('network', 'camera0')
          self.camera_port = cfg.getint('network', 'port')
          self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
          self.sock.connect((self.camera_ip, self.camera_port))
          self.port = str(self.sock.getsockname()[1])
          self.rfile = open(self.sock.fileno(), 'rt', encoding = 'latin-1',
                            closefd = False)
          self.wfile = open(self.sock.fileno(), 'wt', encoding = 'latin-1',
                            closefd = False)
  
      def run(self):
          for i in range(self.total_scan_pass):
              print(self.port)
              self.reply = b''
              self.reply = send_cmd('getImageInfo\n', self.rfile, self.wfile)
              print(self.reply)
              while self.reply == '-1 0 0 0 0':
                  self.reply = send_cmd('getImageInfo\n', self.rfile, self.wfile)
                  print(self.reply)
              self.reply_tokens = self.reply.split(' ')
              self.pixels_num = (int(self.reply_tokens[3]) -
                                 int(self.reply_tokens[1]) + 1)
              self.lines_num = (int(self.reply_tokens[4]) -
                                int(self.reply_tokens[2]) + 1)
  
              self.reply = b''
              self.reply = send_cmd('getImageData\n', self.rfile, self.wfile)
              self.recv_buf = b''
              self.recv_buf = recvall(self.sock, self.pixels_num * self.lines_num)
  </code></pre>
  
  <p>Initialization:</p>
  
  <pre><code>irp_list = []
  for i in range(8):
      irp_instance = irp(cfg, 133)
      irp_list.append(irp_instance)
  </code></pre>