Python urllib-多处理中的虚假输入数据包丢失

Python urllib-多处理中的虚假输入数据包丢失,python,urllib,connection-timeout,Python,Urllib,Connection Timeout,我在使用Python 3和urllib时遇到了一个奇怪的问题 我有一个Python应用程序,它以多个进程(大约5到8个实例)的形式运行,使用Popen启动 每隔一段时间,这些进程会连接到IP摄像头,以尝试获取图像 使用一个摄像头时效果很好,但从4个摄像头开始,我会出现随机连接错误。所有摄像头都连接到一组PoE开关。我已经在开关之间移动了摄像头,看看它是否会对错误产生影响,但错误基本相同,因此我不想看起来与硬件相关 虽然错误似乎不是由相机引起的(仅此一项工作没有缺陷),但错误的数量直接取决于相机。

我在使用Python 3和urllib时遇到了一个奇怪的问题

我有一个Python应用程序,它以多个进程(大约5到8个实例)的形式运行,使用Popen启动

每隔一段时间,这些进程会连接到IP摄像头,以尝试获取图像

使用一个摄像头时效果很好,但从4个摄像头开始,我会出现随机连接错误。所有摄像头都连接到一组PoE开关。我已经在开关之间移动了摄像头,看看它是否会对错误产生影响,但错误基本相同,因此我不想看起来与硬件相关

虽然错误似乎不是由相机引起的(仅此一项工作没有缺陷),但错误的数量直接取决于相机。我的意思是,一旦有3个或4个以上的摄像头并联,错误就开始发生,错误的数量与摄像头有关。。。例如,摄像头1会在20%的请求中失败,摄像头2会在4%的请求中失败,摄像头3会在5%的请求中失败,无论连接了哪些摄像头,这一点都不会改变

如果我不添加超时,它可能会在几秒钟后失败,直到整整几天,并出现各种错误:WinError 10060 10061和10054

请注意,所有进程几乎同时启动一个请求。但我无法想象这是错误的根本原因,因为8个连接请求并不多

当我查看Wireshark跟踪时,我发现当发生错误时,传入的数据包似乎不知何故丢失了。我不认为这是摄像头的错误,因为当电脑一次只能访问一个摄像头时,摄像头就会工作,而丢失的信息来自摄像头

这就像消息在缓冲区中的某个地方,但在urllib超时之前未被注意到一样

这是正常连接:

#
# Read the sensor data
#
def _ReadSensor(self):

    command = 'http://{0}/record/current.jpg'.format(self.link_ip)

    # Time metering
    tic = time.time()

    # Read camera
    try:
        response = urllib.request.urlopen(command, timeout=self._read_timeout)

    except Exception as e:
        duration1 = time.time() -  tic
        raise Exception("Failure to read image from camera \'{0}\' @ {1} ({2}s until exception)...\n{3}".format(self.sensor_name, self.link_ip, duration1, e))

    else:

        # Return response
        try:
            duration2 = time.time() -  tic
            return response.read()

        except Exception as e:
            duration3 = time.time() -  tic
            raise Exception("Failure to read from connection from camera \'{0}\' @ {1} ({2}s until read, {3}s until exception)...\n{4}".format(self.sensor_name, self.link_ip, duration2, duration3, e))

        finally:
            response.close()

如果失败,则为:

#
# Read the sensor data
#
def _ReadSensor(self):

    command = 'http://{0}/record/current.jpg'.format(self.link_ip)

    # Time metering
    tic = time.time()

    # Read camera
    try:
        response = urllib.request.urlopen(command, timeout=self._read_timeout)

    except Exception as e:
        duration1 = time.time() -  tic
        raise Exception("Failure to read image from camera \'{0}\' @ {1} ({2}s until exception)...\n{3}".format(self.sensor_name, self.link_ip, duration1, e))

    else:

        # Return response
        try:
            duration2 = time.time() -  tic
            return response.read()

        except Exception as e:
            duration3 = time.time() -  tic
            raise Exception("Failure to read from connection from camera \'{0}\' @ {1} ({2}s until read, {3}s until exception)...\n{4}".format(self.sensor_name, self.link_ip, duration2, duration3, e))

        finally:
            response.close()

代码如下:

#
# Read the sensor data
#
def _ReadSensor(self):

    command = 'http://{0}/record/current.jpg'.format(self.link_ip)

    # Time metering
    tic = time.time()

    # Read camera
    try:
        response = urllib.request.urlopen(command, timeout=self._read_timeout)

    except Exception as e:
        duration1 = time.time() -  tic
        raise Exception("Failure to read image from camera \'{0}\' @ {1} ({2}s until exception)...\n{3}".format(self.sensor_name, self.link_ip, duration1, e))

    else:

        # Return response
        try:
            duration2 = time.time() -  tic
            return response.read()

        except Exception as e:
            duration3 = time.time() -  tic
            raise Exception("Failure to read from connection from camera \'{0}\' @ {1} ({2}s until read, {3}s until exception)...\n{4}".format(self.sensor_name, self.link_ip, duration2, duration3, e))

        finally:
            response.close()
同样:代码是从以Popen启动的独立进程运行的。引发的异常是第一个异常(“无法从…读取图像”)

当然,我也会不时在其他地方遇到其他错误,但99%的情况下都会发生这种情况

这也是设置相机属性的代码,相机有时会出现故障,但大部分时间都能正常工作。(代码是读取凸轮->设置凸轮->读取凸轮->设置凸轮->等等)

如果你需要更多信息,请告诉我!谢谢