Messaging Patterns LabPython · ZeroMQ

Chapter 2

Preserve message boundaries

Bytes, frames and messages are different units.

What you’ll learn

Check which pieces of a multipart message arrive and how many bytes they contain.

Prerequisite: Send a request to another process. Course map →

The situation

A sender joins fields with a separator. The receiver misreads a value that happens to contain that separator.

A frame is one piece of a ZeroMQ message. send_multipart sends a sequence of frames together. recv_multipart returns the pieces in order. The example displays each byte as two hexadecimal digits so a frame can contain values such as 00 and ff.

Frame boundaries survive the exchange
One multipart message
  1. Topic bytesorders
  2. Binary bytes · hex00 ff
  3. Empty frame0 bytes

Conceptual diagram. Three frames form one ZeroMQ message. Two PAIR sockets pass it through an address on the guest’s filesystem.

Before you run

How many frames are sent? How many payload bytes do they contain before any overhead from ZeroMQ?

Run the experiment

Two PAIR sockets connect directly to each other over a local socket path. The sender sends the text orders, two binary bytes and an empty frame.

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

frame_count is 3 and payload_bytes is 8. The last entry in frames is an empty string, which represents a frame containing zero bytes.

Full output from the original example
{
  "frames": [
    "6f7264657273",
    "00ff",
    ""
  ],
  "frame_count": 3,
  "payload_bytes": 8
}

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

Try one change

Replace the last line with result = multipart(extra_empty=False). Predict frame_count and payload_bytes, then click Run again to see which count changes.

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 multipart(extra_empty=True):
    with tempfile.TemporaryDirectory(prefix='mpl-frames-') as directory,lab() as socket:
        endpoint='ipc://'+directory+'/socket'
        a=socket(zmq.PAIR);b=socket(zmq.PAIR);a.bind(endpoint);b.connect(endpoint)
        frames=[b'orders',b'\x00\xff',b''] if extra_empty else [b'orders',b'\x00\xff']
        a.send_multipart(frames);received=b.recv_multipart()
        return {'frames':[v.hex() for v in received],'frame_count':len(received),'payload_bytes':sum(map(len,received))}
Costs, waiting and recovery
AspectWhat to account for
WorkMore frames and bytes give the sender and receiver more to process. The hexadecimal display takes two characters per byte.
Stored informationThe example receives the whole message before inspecting it. Set limits on both message size and the number of frames.
Message pathThe sockets use a local filesystem path inside one process.
WaitingThe receive returns the list of frames for one complete message.
RecoveryA system needs a saved copy if receivers must replay a message after a restart.

Check your understanding

A team sends a JSON object in three frames. What does the receiver still have to agree with the sender?

Show hints
  1. Hint 1

    What does the frame sequence tell the receiver?

  2. Hint 2

    What must the receiver know to interpret the bytes inside each frame?

Show explanation

Agree on which frame contains each part, the encoding, size limits and how to handle invalid values. ZeroMQ preserves the frame sequence. The application interprets its contents.

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.

Experiment scope

Both PAIR sockets use IPC, a local operating-system connection between sockets. Each socket belongs to the same Python process in this example.

ZeroMQ reference · Runtime and model boundaries