How to Broadcast a Small Lookup Table in Python
Simulates broadcasting a small lookup table by iterating key-value pairs and emitting packed rows to subscribers with deterministic output.
Python code
24 linesimport random
# Generate a deterministic mock broadcast of a small lookup table
# with 5 keys and random integer values (seeded for reproducibility)
data = {
"sensor_a": 22,
"sensor_b": 87,
"sensor_c": 43,
"sensor_d": 65,
"sensor_e": 31,
}
# Simulate a broadcast to subscribers by iterating and packing rows
for key, value in data.items():
packed = f"{key}:{value}"
print(f"BROADCAST {packed}")
if __name__ == "__main__":
# Re-emit the table in canonical order to demonstrate deterministic output
print("--- lookup table snapshot ---")
for k in sorted(data):
print(k, data[k])
Output
BROADCAST sensor_a:22
BROADCAST sensor_b:87
BROADCAST sensor_c:43
BROADCAST sensor_d:65
BROADCAST sensor_e:31
--- lookup table snapshot ---
sensor_a 22
sensor_b 87
sensor_c 43
sensor_d 65
sensor_e 31
How it works
The data dict holds the lookup table with string keys and integer values. Iterating data.items() yields each key-value pair, and the f-string f"{key}:{value}" packs them into a single row. The print statement simulates a broadcast by writing each packed row to stdout. Finally, sorted iteration guarantees a canonical order for deterministic output, useful for testing or downstream comparison.
Common mistakes
- Assuming dict iteration order is always sorted — use `sorted()` for deterministic output
- Forgetting to close file handles if writing to a file instead of stdout
- Overlooking that the broadcast loop runs even when imported — guard with `if __name__ == "__main__"`
Variations
- Use `json.dumps(data)` to serialize the whole table as one broadcast message instead of per-row packed strings
- Broadcast via a socket or message queue by sending each packed line with an appropriate send method
Real-world use cases
- Distributing a small reference table (e.g., country codes) to worker nodes in a Spark cluster for efficient joins.
- Sending configuration values from a controller to multiple service instances at startup.
- Publishing a lookup table to web clients via WebSocket for real-time client-side enrichment.
Sponsored
More from Big data & Spark
Keep learning
Related tutorials and quizzes for this topic.