1

I have the following code sending array from the server to the client using req and reply pattern,

def send_array( socket, A, flags = 0, copy = True, track = False ):
 """send a numpy array with metadata"""
 md = dict( dtype = str( A.dtype ),
           shape = A.shape,)
 socket.send_json( md, flags | zmq.SNDMORE )
 return socket.send(A,  flags, copy = copy, track = track )

 context = zmq.Context()
 socket = context.socket(zmq.REQ)
 socket.connect("tcp://localhost:5667")
 videoFile = "C:/Users/Downloads/test.mp4"
 camera = cv2.VideoCapture(videoFile)
 while True:
   grabbed, frame = camera.read()
  try:
   frame = cv2.resize( frame, (224, 224) ).astype( "float32" )
  except cv2.error:
  break
  image= img_to_array(frame)
  image=image.reshape((1,image.shape[0],image.shape[1],image.shape[2]))
  image=preprocess_input(image)
  preds=model.predict(image)
  send_array(socket, preds)
 socket.close()

def recv_array( socket, flags = 0, copy = True, track = False ):
  md = socket.recv_json( flags = flags )
  msg = socket.recv(flags = flags, copy = copy, track = track )
  #buf = buffer( msg )
  #pass;
  #img = np.frombuffer(bytes(memoryview(msg)), dtype=md['dtype'])
  A = numpy.frombuffer( bytes(memoryview(msg)), dtype = md['dtype'] )
  return A.reshape(md['shape'])

context = zmq.Context()
socket = context.socket(zmq.REP)
socket.bind("tcp://*:5667")
time.sleep(0.2)

while True:
  frame  = recv_array(socket)
  print(frame)
socket.close()

I am not retrieving any error but no data is sent from the client to the server. Thanks, help is highly appreciated.

gohar shah
  • 165
  • 1
  • 10

1 Answers1

2

Welcome to the lands of art of Zen-of-Zero.

Using a ZeroMQ REQ/REP Scalable Formal Communication Pattern Archetype, one has to meet it's documented properties, or the work fails to complete.

The very REQ/REP Archetype is documented to use and follow a state-full, distributed Finite State Automaton ( dFSA ), where REQ.send()-asks, which next REP.recv()-s and must REP.send()-answer, which must be REQ.recv()-ed, before any REQ can make any next .send() and this dFSA-loop repeats ad infinitum (or until an unsalvageable mutual deadlock appear - for more details see this and this).

So, your code shall never omit keeping a REQ-side two-step dance of :
.send()-.recv()-.send()-.recv()-... and,
symmetrically
a REP-side .recv()-.send()-.recv()-.send()-...

Not doing so will hang the flow of the state-full transitions of the said dFSA and the code gets left returning EFSM-error state from any such call to a { .recv() | .send() }-method, that comes not aligned with the REQ/REP regular sequence zipped inside the said dFSA.

Last but not least, your code as-is, is weak and crash-prone in never testing return value, having thus no adaptive handling of warnings, like that the multipart-message did not get read till the very end (or missing a next, just assumed and blindly believed in, multipart-message part). This will crash a code in a similar, yet the very same painful, way.

is by far more complex, than a local, pure [SERIAL]-kind of code.

halfer
  • 19,824
  • 17
  • 99
  • 186
user3666197
  • 1
  • 6
  • 50
  • 92