Eclipse Zenoh Python API

1.9.0 · active · verified Thu Apr 16

Eclipse Zenoh (pronounce /zeno/) is a lightweight, high-performance middleware that unifies data in motion, data at rest, and computations across diverse environments, from embedded systems to the cloud. The Python API (version 1.9.0) provides bindings to the core Rust implementation, enabling pub/sub, geo-distributed storage, queries, and computations. It maintains an active development and release cadence, with major updates often quarterly, aiming for performance and efficiency beyond mainstream stacks.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates a simple Zenoh publisher that periodically sends random temperature readings to the 'myhome/kitchen/temp' key expression. It uses a context manager to ensure the Zenoh session is properly closed. To observe the data, you would run a separate Zenoh subscriber.

import zenoh
import time
import random
import os

# Configure a Zenoh session (default configuration for local peer/client)
# For a router or specific configuration, consider loading from a file:
# config_file = os.environ.get('ZENOH_CONFIG_FILE', '')
# if config_file: config = zenoh.Config.from_file(config_file)
# else: config = zenoh.Config()

def read_temp():
    return random.randint(15, 30)

if __name__ == "__main__":
    # Open a Zenoh session using a context manager for proper cleanup
    with zenoh.open(zenoh.Config()) as session:
        key = 'myhome/kitchen/temp'
        # Declare a publisher on the specified key expression
        pub = session.declare_publisher(key)

        print(f"Zenoh Publisher sending data to '{key}'...")
        try:
            while True:
                t = read_temp()
                buf = f"{t}"
                pub.put(buf)
                print(f"Putting Data ('{key}': '{buf}')...")
                time.sleep(1)
        except KeyboardInterrupt:
            print("Publisher stopped.")

view raw JSON →