Messaging Patterns LabPython · ZeroMQ

Chapter 3

Respect the REQ state machine

A request socket waits for its reply before sending again.

What you’ll learn

Find the error from a second send and show how receiving the first reply lets the client send again.

Prerequisite: Preserve message boundaries. Course map →

The situation

A client sends twice because its first request is slow. The second send fails before any second business operation can be requested.

A request socket (REQ) normally alternates between sending and receiving. A reply socket (REP) receives and then sends. A REQ send before its expected reply raises EFSM, an error that means the current step does not allow that operation. DONTWAIT asks an operation to return instead of waiting. It still follows the send and receive order.

A REQ socket waits for its reply before sending again
  1. ReadySend requestThe socket now expects a reply.
  2. WaitingReceive replySending again while waiting raises EFSM.
  3. Ready againSend next requestReceiving the reply lets the socket send again.

Conceptual diagram. This diagram shows the default REQ sequence with no special options enabled.

Before you run

Will the send with DONTWAIT succeed while the request socket still expects a reply?

Run the experiment

The example sends the first request and tries the second before receiving the first reply. Then the client receives the reply and sends the second request again.

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 early attempt in second_send reports EFSM. successful_exchanges is 2 because the client completes both requests in send–receive order.

Full output from the original example
{
  "second_send": "EFSM: receive required before another send",
  "first_reply": "ACK",
  "successful_exchanges": 2
}

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

Try one change

Replace the last line in the editor with result = request_state(send_early=False). Predict what changes in second_send and successful_exchanges, then click Run again. What can the client do once it has received the first reply?

Inspect the socket implementation

The editor calls this read-only helper. Read all socket helpers or read the separate-process code. The code download includes both files.

def request_state(send_early=True):
    with lab() as socket:
        server=socket(zmq.REP);client=socket(zmq.REQ);server.bind('inproc://state');client.connect('inproc://state')
        client.send(b'first');server.recv()
        state='Accepted after receiving the first reply'
        if send_early:
            try:client.send(b'second',zmq.DONTWAIT);state='unexpected acceptance'
            except zmq.ZMQError as e:
                if e.errno!=zmq.EFSM:raise
                state='EFSM: receive required before another send'
        server.send(b'ACK');reply=client.recv().decode()
        client.send(b'second');server.recv();server.send(b'ACK');client.recv()
        return {'second_send':state,'first_reply':reply,'successful_exchanges':2}
Costs, waiting and recovery
AspectWhat to account for
WorkThe socket checks the next operation before sending a message.
Stored informationOne ordinary REQ socket keeps one request open at a time.
Message pathThe refused early send never reaches the server.
WaitingReceiving the reply lets the socket send again.
RecoveryIf you replace the socket, check whether the server already handled the first request before you repeat it.

Check your understanding

A request socket reports EFSM when your code tries to retry. What should the code check before attempting another send?

Show hints
  1. Hint 1

    Look at the operation the request socket expects next.

  2. Hint 2

    After a timeout the same socket may still expect the reply.

Show explanation

Check whether the socket is still expecting a receive. Finish that receive within the time limit, or replace the socket as part of a retry policy. A policy also needs to account for what may have happened to the original request on the server.

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 example uses ordinary REQ and REP sockets. Other REQ options can change the sequence rules.

ZeroMQ reference · Runtime and model boundaries