Messaging Patterns LabPython · ZeroMQ

Chapter 14

Choose connection and close policy

See what a socket accepts while no receiver is connected.

What you’ll learn

Compare send acceptance with delivery evidence when a socket closes.

Prerequisite: Bound a socket queue. Course map →

The situation

A job is accepted before a service exists. Closing the process then discards the pending work.

connect starts connecting without waiting for the connection to finish. On this PUSH socket, IMMEDIATE restricts sends to completed connections. The example attempts a send with DONTWAIT, then closes with LINGER=0, which tells ZeroMQ to discard pending outgoing messages on close.

Decide whether to queue work before a connection is ready
connectAsynchronous connection requestThere is no listener on the chosen endpoint.
  1. IMMEDIATE=1A send with DONTWAIT raises EAGAINAdmission requires a completed connection.
  2. IMMEDIATE=0May queue pending workAcceptance supplies no receiver acknowledgement.

Conceptual diagram. Closing with LINGER=0 discards any messages still waiting to be sent. The example checks which send is accepted and whether any reply arrives.

Before you run

What does send_accepted=True prove about receiver processing?

Run the experiment

The example connects to a port inside the guest where no receiver is listening.

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

send_accepted is false in the original because there is no connected receiver. application_acknowledgement is also false.

Full output from the original example
{
  "immediate": true,
  "send_accepted": false,
  "application_acknowledgement": false,
  "close_policy": "LINGER=0; pending work is discarded when this socket closes."
}

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

Try one change

Replace the last line with result = immediate(enabled=False) and click Run again. Compare send_accepted, application_acknowledgement and close_policy. What changes about queue admission and what evidence of delivery do you have?

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 immediate(enabled=True):
    with lab() as socket:
        source=socket(zmq.PUSH,immediate=int(enabled));source.connect('tcp://127.0.0.1:1')
        try:source.send(b'work',zmq.DONTWAIT);accepted=True
        except zmq.Again:accepted=False
        return {'immediate':enabled,'send_accepted':accepted,'application_acknowledgement':False,'close_policy':'LINGER=0; pending work is discarded when this socket closes.'}
Costs, waiting and recovery
AspectWhat to account for
WorkTrying to connect and keeping pending messages use resources even if no work reaches a receiver.
Stored informationDecide how much pending work the sender can keep.
Message pathThe local test port has no listener.
WaitingIMMEDIATE controls whether the socket accepts a send now. LINGER controls what happens to pending messages at close.
RecoveryWhen LINGER is zero, the socket discards pending messages on close.

Check your understanding

Would setting infinite linger be a safe way to guarantee a shutdown finishes all business work?

Show hints
  1. Hint 1

    Can queued messages finish sending if there is no connected receiver?

  2. Hint 2

    What reply would tell the sender that the receiver completed the work?

Show explanation

An unreachable peer can leave pending messages indefinitely. Choose a deadline for shutdown, record unfinished work in a source you can recover, and define what application reply establishes completion.

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

This example closes a socket that has no connected peer. It shows send acceptance and the selected close rule.

ZeroMQ reference · Runtime and model boundaries