Python中多线程多插槽意外关闭

时间:2016-01-27 02:09:41

标签: python c++ linux multithreading sockets

我运行了一个C ++服务器程序,我的Python客户端正试图从中获取一些数据。我想生成多个客户端以同时获取数据。以下是数据获取部分及其初始化的代码。奇怪的是,当数据抓取发生时,一些套接字会意外死亡。据我了解,产生类的方式已经建立了八个独立的连接。它们应该单独运行而不会相互干扰。知道什么是关闭套接字?

数据抓取课程:

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)

初​​始化:

irp_list = []
for i in range(8):
    irp_instance = irp(cfg, 133)
    irp_list.append(irp_instance)

0 个答案:

没有答案