Programming with ZeroMQ

Recently I am working with OpenCV and YOLO. When I was just using OpenCV for realtime object detection and recognization there was not good enough acuracy in results after I search a little bit more for acuracy I find two options TencerFlow and YOLOv3. I firstly preffer YOLO algorithm for my desired accuracy and I got good enough accuracy in my results. But I lost the number of frames per second. I have Intel® Core™ i5-2330M CPU @ 2.20GHz × 4 sytem and I was getting 0.6 frames per second(FPS) which is very slow as compare to normal video frames. Then I search a bit more and I realize that YOLO can run on GPU very smoothly. So I got access to GPU and now I face another problem that how could I send my video frames to GPU which is on remote and in response GPU will send back these frames after processing and how could I recieve these frames. Now the main issue is how could sync my local system and remote in no time. I find ZeroMQ solution to solve this issue. ZeroMQ has very simple structure and it is compatible with C/C++, PHP, Java, Python, Lua, Haxe, C#, CL, Delphi, Erlang, F#, Felix, Haskell, Objective-C, Ruby, Ada, Basic, Clojure, Go, Haxe, Node.js, ooc, Perl, and Scala. ZeroMQ (also known as ØMQ, 0MQ, or zmq) looks like an embeddable networking library but acts like a concurrency framework. It gives you sockets that carry atomic messages across various transports like in-process, inter-process, TCP, and multicast. Following figure will show its working in simplest way.

Client – Server Structure

I would like to share some code snipts of ZeroMQ. So firstly pip should be installed in your system. If pip is not installed before follow the following command else just skip this out.

~ apt install python-pip	#python 2
~ apt install python3-pip	#python 3

Now you need to install ZeroMQ on both local and remote machines. For installtion type the following command:

pip install pyzmq

Now make a server.py file on to your server in my case it is remote GPU and put the following code into your file.

#
#   Hello World server in Python
#   Binds REP socket to tcp://*:5555
#

import time
import zmq

context = zmq.Context()
socket = context.socket(zmq.REP)
socket.bind("tcp://*:5555")

while True:
    #  Wait for next request from client
    msg = socket.recv()
    print("Received request: %s" % msg)

    #  Do something else
    time.sleep(1)

    #  Send reply back to client
    socket.send(b"World!")

Now make a client.py file on to your local machine and put the following code into your file.

#
#  Client file
#   Hello World client in Python
#   Connects REQ socket to tcp://your_server_IP:5555
#
import zmq

context = zmq.Context()

#  Socket to talk to server
print("Connecting to Hello-World server…")
socket = context.socket(zmq.REQ)
socket.connect("tcp://121.52.146.109:5555")

#  Do unlimited requests, waiting each time for a response
request = 0
while True:
    print("Sending request %s …" % request)
    socket.send_string("Hello")
    #  Get the reply form server
    message = socket.recv()
    print("Received reply %s [ %s ]" % (request, message))
    request +=1

Now go to your directories in which you have created these files. Now on local machine run the code by typing following command:

python client.py

And similarly run the code on remote side by typing following command:

python server.py

Following is the resulting output of this code. Left output is of remote and right output is of client.

5 thoughts on “Programming with ZeroMQ

  1. An outstanding share! I have just forwarded this onto a co-worker who had been conducting a little research on this.
    And he actually bought me breakfast due to the fact that I stumbled upon it for him…

    lol. So allow me to reword this…. Thank
    YOU for the meal!! But yeah, thanx for spending some time to
    discuss this subject here on your blog.

    Liked by 1 person

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.