Multiprocess & pyzmq

You will have noticed in the previous examples for the REQUEST-REPLY pattern that we executed the server and client separately. This was due to the fact that each program had a while loop that would run for ever. Only way is to invoke these little programs separately.

This served well to understand the various ØMQ patterns and devices. How ever, it is far better to use multiprocessing module.

This part of tutorial has nothing to do with ØMQ but how we use it with python programs.

request_reply_processes.py

import zmq
import time
import sys
from  multiprocessing import Process

def server(port="5556"):
    context = zmq.Context()
    socket = context.socket(zmq.REP)
    socket.bind("tcp://*:%s" % port)
    print "Running server on port: ", port
    # serves only 5 request and dies
    for reqnum in range(5):
        # Wait for next request from client
        message = socket.recv()
        print "Received request #%s: %s" % (reqnum, message)
        socket.send("World from %s" % port)
         
def client(ports=["5556"]):

    context = zmq.Context()
    print "Connecting to server with ports %s" % ports
    socket = context.socket(zmq.REQ)
    for port in ports:
        socket.connect ("tcp://localhost:%s" % port)
    for request in range (20):
        print "Sending request ", request,"..."
        socket.send ("Hello")
        message = socket.recv()
        print "Received reply ", request, "[", message, "]"
        time.sleep (1) 


if __name__ == "__main__":
    # Now we can run a few servers 
    server_ports = range(5550,5558,2)
    for server_port in server_ports:
        Process(target=server, args=(server_port,)).start()
        
    # Now we can connect a client to all these servers
    Process(target=client, args=(server_ports,)).start()

Now it easy to run the server and clients in one go.

The output shows how the requests are load balanced across available servers:

Running server on port:  5550
Running server on port:  5552
Running server on port:  5554
Running server on port:  5556
Connecting to server with ports [5550, 5552, 5554, 5556]
Sending request  0 ...
Received request #0: Hello
Received reply  0 [ World from 5550 ]
Sending request  1 ...
Received request #0: Hello
Received reply  1 [ World from 5552 ]
Sending request  2 ...
Received request #0: Hello
Received reply  2 [ World from 5554 ]
Sending request  3 ...
Received request #0: Hello
Received reply  3 [ World from 5556 ]
Sending request  4 ...
Received request #1: Hello
Received reply  4 [ World from 5550 ]