Back to Asynchronous Broadcast

Interprocess Communication (IPC) · Asynchronous Broadcast · Practice Question

Broadcast an Async Message to Multiple Handles

Sign in to track your progress

Question

What is an asynchronous broadcast, and how does it differ from sending the same message to each handle individually?

Solution

handles:hopen each 8001 8002     / two processes listening on ports 8001 and 8002
msg:(`upd;`trade;([] sym:enlist`AAPL; price:enlist 185.2))
-25!(handles;msg)

Alternative Solution

neg[handles]@\:msg

Explanation

-25! queues the same async message for delivery on a list of handles at once. neg[handles]@\:msg does the same job with each-left, but serialises msg separately for every handle. -25! serialises it once and reuses that for every handle — much cheaper for fanning the same update out to many connections, like a tickerplant pushing ticks to all its subscribers.

Since one serialised form has to suit every recipient, -25! serialises to the lowest IPC capability among the target handles (relevant when the recipients run different kdb+ versions). If there is an error, no messages are sent, and -25! returns the handle that caused it.

Like any async send, the message is only queued, not necessarily flushed immediately. It can be flushed manually:

neg[handles]@\:(::)
/ or
-25!(handles;::)