python zeromq request response

Python ZeroMQ Request Response

If you're looking for a way to establish a messaging system between two or more entities, such as applications or processes, ZeroMQ is a good choice. It's a lightweight and fast messaging library that supports various messaging patterns, including request-response, publish-subscribe, and pipeline.

In Python, you can use the PyZMQ library to interface with ZeroMQ. Here's how you can implement a request-response pattern using ZeroMQ in Python:

Server-side code

The server-side code creates a ZeroMQ socket and binds it to an endpoint, such as a TCP port or a Unix domain socket. Then it listens for incoming requests and sends responses back.


import zmq

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

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

    # Send reply back to client
    socket.send(b"Hello from server")
  

The code above creates a ZeroMQ context and socket, binds it to a TCP port 5555, and enters an infinite loop waiting for incoming requests. When a request arrives, it prints the message to the console and sends a response back.

Client-side code

The client-side code creates a ZeroMQ socket and connects it to the server's endpoint. Then it sends requests and waits for responses.


import zmq

context = zmq.Context()
socket = context.socket(zmq.REQ)
socket.connect("tcp://localhost:5555")

# Send request to server
socket.send(b"Hello from client")

# Wait for reply from server
message = socket.recv()
print("Received reply: %s" % message)
  

The code above creates a ZeroMQ context and socket, connects it to the server's TCP port 5555, sends a request message "Hello from client", and waits for a response message. When the response arrives, it prints the message to the console.

Alternative way to implement

Another way to implement the request-response pattern using ZeroMQ in Python is to use the REQ-REP socket pair in a non-blocking mode. In this mode, the sender and receiver can send and receive messages without blocking each other.


import zmq
import time

context = zmq.Context()

# Create client socket and connect to server
client = context.socket(zmq.REQ)
client.connect("tcp://localhost:5555")

# Create server socket and bind to endpoint
server = context.socket(zmq.REP)
server.bind("tcp://*:5555")

# Poller to handle both sockets
poller = zmq.Poller()
poller.register(client, zmq.POLLIN)
poller.register(server, zmq.POLLIN)

# Request-response loop
while True:
    socks = dict(poller.poll())

    # Handle incoming request from client
    if server in socks:
        message = server.recv()
        print("Received request: %s" % message)
        time.sleep(1)  # Simulate processing delay
        server.send(b"Hello from server")

    # Handle incoming reply from server
    if client in socks:
        message = client.recv()
        print("Received reply: %s" % message)

        # Send a new request to server
        client.send(b"Hello from client")
  

The code above creates a ZeroMQ context and two sockets, a client socket and a server socket. The client socket connects to the server's TCP port 5555, and the server socket binds to the endpoint. Then, the code creates a poller to handle both sockets and enters an infinite loop waiting for incoming messages from either socket. When a message arrives, it prints the message to the console, simulates a processing delay (for the server side), sends a response back (for the server side), or sends a new request (for the client side).