无法连接以在 openzmq 中的现有 tcp 连接中打开另一个 tcp 连接



我正在从客户端 1 和客户端 2 连接到客户端 2 到服务器。我正在将帧从客户端 1 发送到客户端 2,在客户端 2 上,我正在执行预测并将结果发送到服务器。我有以下代码。

客户端 1 代码:

context = zmq.Context()
footage_socket = context.socket(zmq.PUB)
footage_socket.connect('tcp://172.168.1.2:5555')
videoFile = 'data.mp4'
camera = cv2.VideoCapture(videoFile) 
length=int(camera.get(cv2.CAP_PROP_FRAME_COUNT))
print(length)
count=0
#time.sleep(2)
while True:        
grabbed, frame = camera.read()
count+=1
print(count)
try:
frame = cv2.resize(frame, (224, 224))
except cv2.error:
break
encoded, buffer = cv2.imencode('.jpg', frame)
jpg_as_text = base64.b64encode(buffer)
footage_socket.send(jpg_as_text)

客户端 2 代码:

context = zmq.Context()
footage_socket = context.socket(zmq.SUB)
footage_socket.bind('tcp://0.0.0.0:5555')
footage_socket.setsockopt_string(zmq.SUBSCRIBE, np.unicode(''))
while True:    
frame = footage_socket.recv_string()
img = base64.b64decode(frame)
npimg = np.fromstring(img, dtype=np.uint8)
source = cv2.imdecode( npimg, 1 )
frame=cv2.resize(source,(224,224)).astype("float32")
image = img_to_array( source)
image = image.reshape( (1, image.shape[0], image.shape[1], image.shape[2]) )
image = preprocess_input( image )
preds = model.predict(image)
##connecting to server##        
context1=zmq.Context()
footage_socket=context1.socket(zmq.PUB)
footage_socket.connect('tcp://192.168.56.103:9999')
footage_socket.send(preds)
print('sending to server')

服务器代码:

context = zmq.Context()
footage_socket = context.socket(zmq.SUB)
footage_socket.bind('tcp://0.0.0.0:9999')
footage_socket.setsockopt_string(zmq.SUBSCRIBE, np.unicode(''))
while True:
frame = footage_socket.recv_string()
img = base64.b64decode(frame)
#print(img)

在客户端 2 上,我收到以下错误

frame = footage_socket.recv_string()
File "/usr/local/lib/python3.5/dist-packages/zmq/sugar/socket.py", line 583, in recv_string
msg = self.recv(flags=flags)
File "zmq/backend/cython/socket.pyx", line 790, in zmq.backend.cython.socket.Socket.recv
File "zmq/backend/cython/socket.pyx", line 826, in zmq.backend.cython.socket.Socket.recv
File "zmq/backend/cython/socket.pyx", line 193, in zmq.backend.cython.socket._recv_copy
File "zmq/backend/cython/socket.pyx", line 188, in zmq.backend.cython.socket._recv_copy
File "zmq/backend/cython/checkrc.pxd", line 25, in zmq.backend.cython.checkrc._check_rc
zmq.error.ZMQError: Operation not supported

几个罪过,让我们一个接一个地揭开面具:

客户端1 可以改进,但客户端 2 存在大多数问题:

################################################################### FOOTAGE ~ <SUB>-Socket
# SUB
footage_socket = context.socket( zmq.SUB )
...
PUB_TARGET = 'tcp://192.168.56.103:9999'
while True:    
frame  = footage_socket.recv_string()                         # <SUB>.recv()-ed
source = cv2.imdecode( np.fromstring( base64.b64decode( frame ),
dtype = np.uint8
),
1 )
frame  = cv2.resize( source,
(224,224)
).astype( "float32" )
image = img_to_array( source )
image = image.reshape( ( 1,
image.shape[0],
image.shape[1],
image.shape[2]
)
)
preds = model.predict( preprocess_input( image ) )
################################################################## PER-LOOP INFty-times
## connecting to server ###########################
context1=zmq.Context()                           ## INSTANTIATED new Context()-instance
footage_socket = context1.socket( zmq.PUB )      ## ASSOCIATED a new  Socket()-instance
footage_socket.connect( PUB_TARGET )             ## .CONNECT( PUB_TARGET )
footage_socket.send( preds )                     ## <PUB>.send()
################################################### LOOP AGAIN
###################################################      yet now the <PUB>.recv()

a)while True:代码块创建尽可能多的Context()实例,具有所有分配和至少 1 个 I/O 线程,循环运行的次数与循环一样多

b) 循环的尾随部分为footage_socket分配一个新对象(socket类的另一个新实例),使对SUB类型套接字实例的原始对象引用成为孤立实例,但所有关联的资源分配未终止

c) 一个新重新分配的footage_socket-socket,现在带有PUBsocket-实例的引用,确实无法处理.send()-方法,正如while True:代码块的头部所编写的那样,并且抛出(未处理的)异常,如上所述。

<小时 />

解决方案?

消除这些概念错误 - 避免预期SUB和新PUB的名称冲突,避免使代码重复(无限期地)生成新的、新的和新的Context()实例(这些操作在资源方面和延迟方面是昂贵的)及其无限多个socket(PUB)实例和无限多个.connect()-s,否则代码应按预期工作。


您想阅读有关ZeroMQ的更多帮助吗?

然后随意阅读这个答案。

最新更新