简体   繁体   English

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

[英]Unable to connect to open another tcp connection in an existing tcp connection in openzmq

I am connecting to client-2 from client-1 and client-2 to server.我正在从客户端 1 和客户端 2 连接到客户端 2 到服务器。 I am sending frames from the client-1 to client-2 and on client-2, I am performing prediction and sending the result to the server.I have the following code.我正在从客户端 1 向客户端 2 发送帧,在客户端 2 上,我正在执行预测并将结果发送到服务器。我有以下代码。

Client-1 code:客户端 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)

Client-2 code:客户端 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')

Server code:服务器代码:

   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)

On client-2 I am receiving the below error在客户端 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

Several sins, let's demask one after another:几大罪过,一一揭开:

Client-1 could be improved, yet the Client-2 has most of the problems: Client-1 可以改进,但 Client-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: -code-block creates as many Context() -instances, having all allocations and at least 1-I/O-thread, as many times the loop runs through a) while True: -code-block 创建尽可能多的Context() -instances,拥有所有分配和至少 1-I/O 线程,与循环运行的次数一样多

b) trailing part of the loop assigns footage_socket with a new object (another new instance of a socket -class), rendering the original object-reference to a SUB -type socket-instance an orphan, yet having all associated resources' allocations unterminated b) 循环的尾随部分将一个新对象( socket类的另一个新实例)分配给footage_socket ,将原始对象引用呈现给一个SUB类型的 socket-instance 一个孤儿,但所有相关资源的分配都未终止

c) a newly re-assigned footage_socket -socket, now bearing a reference of a PUB -type socket -instance indeed cannot process a .send() -method, as scripted in the head of the while True: -code-block and an (unhandled) exception gets thrown, as has been demonstrated above. c) 一个新重新分配的footage_socket -socket,现在带有PUB类型socket的引用 - .send()确实无法处理.send() -方法,如while True:头部的脚本while True: -code-block 和抛出(未处理的)异常,如上所示。


Solution?解决方案?

Remove these conceptual errors - avoiding colliding names for both an intended SUB and a new PUB and avoid making the code repeatedly (ad infinitum) generating new and new and new Context() -instances (these operations are resources-wise and latency-wise expensive) and its infinitely many socket(PUB) -instances and infinitely many .connect() -s thereof & the code shall otherwise work as expected.消除这些概念性错误 - 避免预期SUB和新PUB名称冲突,并避免使代码重复(无限)生成新的、新的和新的Context()实例(这些操作在资源方面和延迟方面都很昂贵) 及其无限多的socket(PUB)实例和无限多的.connect() ,否则代码将按预期工作。


Would you like to read more help on ZeroMQ?您想阅读有关 ZeroMQ 的更多帮助吗?

Then feel free to read this answer .然后随意阅读这个答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM