Messaging Patterns LabPython · ZeroMQ

Chapter 1

Send a request to another process

Connect, send and receive a reply.

What you’ll learn

Send a message to a second Python process. Use its reply to tell whether it handled the message.

Prerequisite: Basic Python functions, lists and dictionaries. Course map →

The situation

An order client reports a successful send. The shop still needs to know whether its order handler received the request.

A ZeroMQ socket is an object that sends or receives messages. The client uses a request socket (REQ). The server uses a reply socket (REP). A reply that starts with ACK, short for acknowledgement, shows the handler received the message and replied.

Follow a message from the client to the peer and back
  1. Client (REQ)Client sends hello to the peerPeer process (REP)
  2. Client waitsPeer returns ACK and helloPeer sends the reply
  3. Client sends BYEClient asks the peer to closePeer receives BYE
  4. Client receives closedPeer replies closedPeer exits

Conceptual diagram. The program starts the peer process. A separate channel tells the client the peer is ready. The request and reply travel over a TCP connection inside the Linux guest. ACK shows the peer replied.

Before you run

Which step tells the client that the server handled the request? Consider connecting, sending and receiving ACK.

Run the experiment

The page starts a second Python process for the server. The client connects to it inside the temporary Linux system and sends hello.

For the original example, click Restore code if it is shown, then click Run. Compare your output with the worked result.

Python prepares in the background while you read. First use can download about 71 MiB. Your code runs only when you select Run; each run starts with clean experiment state.

Your edits are saved in this browser when storage is available.

Troubleshooting

If Python stops responding, restart the environment and try again. Your code and last completed result are kept.

Downloads

Worked result

The reply list contains ACK and hello. Each entry is a frame, a separate piece of the same message. separate_process is true because a second Python process answered.

Full output from the original example
{
  "request": "hello",
  "reply": [
    "ACK",
    "hello"
  ],
  "separate_process": true,
  "transport": "TCP loopback"
}

The build ran the original editor code to produce this output.

Try one change

Replace the last line in the editor with result = round_trip('order-42'). Predict both reply frames. Run again and check which frame changed.

Inspect the socket implementation

The editor calls this read-only helper. A ZeroMQ context is the object a process uses to create sockets. The program starts the server process. A pipe carries its ready message and port number to the client. The ZeroMQ sockets carry the request and reply. Read all socket helpers or read the separate-process code. The code download includes both files.

def round_trip(value='hello'):
    with peer('rep') as (ready,process),lab() as socket:
        client=socket(zmq.REQ);client.connect(f"tcp://127.0.0.1:{ready['port']}")
        client.send_string(value);reply=client.recv_multipart()
        client.send(b'BYE')
        if client.recv()!=b'closed':raise RuntimeError('Closing handshake failed')
        process.wait(timeout=5)
        return {'request':value,'reply':[frame.decode() for frame in reply],'separate_process':True,'transport':'TCP loopback'}
Costs, waiting and recovery
AspectWhat to account for
WorkThe processes exchange the request and reply, then exchange messages to close the session. The Run timer includes Linux and Python startup.
Stored informationEach process uses memory, and sockets can hold queued messages.
Message pathTCP carries the request and reply between processes inside the guest.
WaitingThe example waits until the server is ready, then waits for its reply.
RecoveryThe guest starts fresh on each run. Recovering an order after a crash would require a saved record outside the guest.

Check your understanding

Your order handler saves an order, but its reply never reaches the client. What should the client check before sending the order again?

Show hints
  1. Hint 1

    What evidence does the client have about the first attempt?

  2. Hint 2

    How could the handler recognize the same order on a second attempt?

Show explanation

The client can check the order status if the service provides it. A retry should reuse the same order ID. The handler needs a rule for returning the recorded outcome when it sees that ID again.

ReferencesFunction documentation and helper source

References open in a new tab; course helpers are documented separately from library APIs. PyZMQ API links use version 27.3.0. ZeroMQ manuals are pinned to libzmq 4.3.5.

Experiment scope

The two processes send ZeroMQ messages over TCP loopback, a connection that stays inside the temporary Linux system. ACK confirms that the example handler replied. The two processes then exchange messages to close the connection.

ZeroMQ reference · Runtime and model boundaries