Chapter 13
Bound a socket queue
Watch a sender reach a queue limit.
Count how many sends succeed before the queue fills, then verify each message arrives.
Prerequisite: Receive without blocking. Course map →
The situation
A receiver pauses while a producer keeps admitting work. Unbounded buffering would conceal the capacity problem.
A high-water mark (HWM) is a setting that limits how many messages a socket queues. SNDHWM sets it for sends and RCVHWM sets it for receives. This example connects the sockets inside one process using ZeroMQ’s inproc transport. The exact number of messages accepted depends on how the sockets connect.
- SourcePUSH · SNDHWM=2DONTWAIT returns an error if the send would have to wait.
- Pinned inproc fixture4 accepted messagesIn this inproc run, both socket limits affect the count.
- Next admissionEAGAINThe receiver has not drained yet.
- ReceiverPULL · RCVHWM=2Read every accepted message.
Conceptual diagram. Starter settings are illustrated. This observed inproc capacity is not a universal TCP, byte or process-memory formula.
Before you run
Will a bound of two necessarily mean only two messages across the whole pipe?
Run the experiment
Both high-water marks start at 2. The sender tries up to 100 sends without waiting. Once it gets Again, the receiver reads every admitted message.
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.
Full error details
Run output
Troubleshooting
If Python stops responding, restart the environment and try again. Your code and last completed result are kept.
Downloads
Worked result
The starter admits four messages before Again. accepted_before_again is the number that got in and drained lists the messages the receiver read.
Full output from the original example
{
"hwm_each": 2,
"accepted_before_again": 4,
"drained": [
0,
1,
2,
3
],
"boundary": "Actual inproc PUSH/PULL queue in this pinned libzmq; this is not a TCP byte or RSS limit."
}The build ran the original editor code to produce this output.
Try one change
Set the last line to result = high_water(hwm=1) and click Run. Repeat with result = high_water(hwm=4). Compare accepted_before_again with the number of entries in drained for each run. What happened to the admitted messages?
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 high_water(hwm=2):
if not 1<=hwm<=8:raise ValueError('hwm must be 1..8')
with lab() as socket:
sink=socket(zmq.PULL,rcvhwm=hwm);source=socket(zmq.PUSH,sndhwm=hwm)
sink.bind('inproc://capacity');source.connect('inproc://capacity');sent=0
for i in range(100):
try:source.send_string(str(i),zmq.DONTWAIT);sent+=1
except zmq.Again:break
else:raise RuntimeError('Expected bounded inproc queue')
received=[int(sink.recv()) for _ in range(sent)]
return {'hwm_each':hwm,'accepted_before_again':sent,'drained':received,'boundary':'Actual inproc PUSH/PULL queue in this pinned libzmq; this is not a TCP byte or RSS limit.'}
Costs, waiting and recovery
| Aspect | What to account for |
|---|---|
| Work | Each attempted send takes work, and the receiver drains each admitted message. |
| Stored information | A socket can hold a few large messages or many small ones. |
| Message path | This example uses queues inside a process. TCP can also buffer data in the operating system. |
| Waiting | When the send would wait, Again tells the sender to decide whether to wait or reject the job. |
| Recovery | An in-memory queue loses its contents when its process stops. |
Check your understanding
How could a queue with a low high-water mark still use a lot of memory?
Show hints
Hint 1
Do all messages use the same number of bytes?
Hint 2
Which other buffers hold messages before processing?
Show explanation
A few large messages can consume more memory than many small ones. Set limits on message sizes and on the number of open queues, and measure memory in the system you plan to deploy.
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.
Bound outgoing and incoming queues
SocketOption.SNDHWM(new tab)SocketOption.RCVHWM(new tab)Socket.send_string(new tab)Flag.DONTWAIT(new tab)libzmq ZMQ_SNDHWM (4.3.5)(new tab)Read high-water marks and the pattern-specific response to a full queue.
Course helper implementation
These are functions written for this course, not additional library APIs. Inspect their parameters, operator calls and authored policies. The source shown is generated from the same Python files packaged with Run and Download Python.
Experiment scope
The counts describe these connected sockets inside one process with the pinned ZeroMQ version. Measure TCP buffering and process memory separately for a deployed service.
ZeroMQ reference · Runtime and model boundaries