Chapter 4
Send work down a pipeline
Send work in one direction and check what reached the receiver.
Compare which jobs the receiver sees with the number of application acknowledgements.
Prerequisite: Respect the REQ state machine. Course map →
The situation
A producer queues six jobs and announces that the batch is done. The receiver has not yet accounted for their processing or persistence.
A PUSH socket sends jobs and a PULL socket receives them. This makes a one-way path, called a pipeline. If the sender needs a reply after the receiver finishes a job, the application must provide a return path.
- ProducerPUSHQueue numbered jobs.
- TransportOne-way pipePass complete messages in one direction.
- WorkerPULLReceive each job’s number and value.
- ResultSum the received valuesThe producer gets no completion reply.
Conceptual diagram. The sender and receiver use PUSH and PULL. The printed total comes from the receiver’s inputs.
Before you run
Will completing all sends produce six application acknowledgements?
Run the experiment
One sender and one receiver connect inside the Python process. The sender queues jobs 0 through 5. Each job carries its number and that number squared. The receiver reads every queued job.
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
jobs lists the jobs the receiver read and total is the sum of their squared values. application_acknowledgements is 0 because this example provides only a one-way connection.
Full output from the original example
{
"jobs": [
0,
1,
2,
3,
4,
5
],
"total": 55,
"application_acknowledgements": 0
}The build ran the original editor code to produce this output.
Try one change
Replace the last line with result = pipeline(count=3). Predict the values in jobs and total, then click Run again to check your calculation.
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 pipeline(count=6):
if not 1<=count<=32:raise ValueError('count must be 1..32')
with lab() as socket:
sink=socket(zmq.PULL);source=socket(zmq.PUSH);sink.bind('inproc://jobs');source.connect('inproc://jobs')
for i in range(count):source.send_json({'job':i,'value':i*i})
rows=[sink.recv_json() for _ in range(count)]
return {'jobs':[r['job'] for r in rows],'total':sum(r['value'] for r in rows),'application_acknowledgements':0}
Costs, waiting and recovery
| Aspect | What to account for |
|---|---|
| Work | Each job adds a send and a receive. JSON encoding also takes time. |
| Stored information | The result stores one entry per job. Unread jobs occupy space in the socket queues. |
| Message path | Both sockets operate inside one process. |
| Waiting | The sender learns that the socket accepted the job. The receiver’s application would need to report finished work. |
| Recovery | The application needs a job record to decide what to repeat after a failure. |
Check your understanding
A worker exits right after receiving a job. What should the dispatcher record so it can decide whether to send the job again?
Show hints
Hint 1
What does receiving the job tell you about whether the worker finished it?
Hint 2
What record would survive if the worker stopped?
Show explanation
Give each job a stable ID, track which worker owns it, and ask the worker to report its outcome. Set a time limit for waiting. If the job must survive a crash, keep the record in persistent storage and decide how to handle the same job ID more than once.
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.
Push jobs and pull them
Socket.send_json(new tab)Socket.recv_json(new tab)SocketType.PUSH(new tab)SocketType.PULL(new tab)libzmq ZMQ_PUSH (4.3.5)(new tab)Read serialization separately from the PUSH/PULL distribution pattern.
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
This example uses one connected PUSH and PULL pair inside a process. It shows which jobs the receiver reads. A system with multiple workers needs separate tests for how jobs are shared.
ZeroMQ reference · Runtime and model boundaries