Feature: Synchronous Low‑Level API for ezmsg (ROS2‑style ergonomics)#215
Merged
griffinmilsap merged 29 commits intodevfrom Feb 26, 2026
Merged
Feature: Synchronous Low‑Level API for ezmsg (ROS2‑style ergonomics)#215griffinmilsap merged 29 commits intodevfrom
griffinmilsap merged 29 commits intodevfrom
Conversation
KonradPilch
approved these changes
Feb 18, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
Adds a synchronous “ROS2-style” low-level API layer on top of ezmsg’s async pub/sub, plus examples/tests, and introduces explicit control over GraphServer auto-start behavior via auto_start.
Changes:
- Introduces
ezmsg.core.sync(SyncContext,SyncPublisher,SyncSubscriber,init,spin,spin_once) backed by a background asyncio loop thread. - Extends GraphServer bring-up behavior with
GraphContext(auto_start=...)andGraphService.ensure(auto_start=...). - Adds split sync/async low-level examples, sync API tests, and a perf comparison script.
Reviewed changes
Copilot reviewed 11 out of 11 changed files in this pull request and generated 8 comments.
Show a summary per file
| File | Description |
|---|---|
src/ezmsg/core/sync.py |
New synchronous wrapper API around GraphContext/Publisher/Subscriber, including callback-based spinning. |
src/ezmsg/core/graphserver.py |
Adds auto_start override to GraphService.ensure and logs GraphServer startup. |
src/ezmsg/core/graphcontext.py |
Plumbs auto_start through GraphContext to GraphService.ensure. |
src/ezmsg/core/__init__.py |
Exposes the new sync API/module from ezmsg.core. |
examples/simple_publisher.py |
New sync publisher example using ez.sync.init. |
examples/simple_subscriber.py |
New sync subscriber example using spin(). |
examples/simple_async_publisher.py |
New async publisher example (replacement for prior combined example). |
examples/simple_async_subscriber.py |
New async subscriber example (replacement for prior combined example). |
examples/lowlevel_api.py |
Removes the previous combined low-level example in favor of split examples. |
tests/test_sync_api.py |
Adds tests for sync autostart/spin_once, backpressure behavior, and CacheMiss handling. |
tests/perf_sync_overhead.py |
Adds a standalone script to measure sync-wrapper overhead vs async usage. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…alculate cur_pubs from self._channels.keys()
…ts-set-of-current-publishers-after-processing-update-preventing-disconnection-of-old-publishers hot fix 217 - recalculate Subscriber's pub keys on demand
Deprecate `zero-copy` keyword argument in `@ez.subscriber`
…le True: try...except KeyboardInterrupt — the same pattern already used for the stop barrier. This ensures SIGINT can't skip context.revert(), which is what cancels the pub/sub internal tasks.
…t; add timeout for context revert second attempt.
Wrap shutdown_units().result() and context.revert().result() with whi…
Fix: Multiple shutdown issues and enhance shutdown UX
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
SyncContext,SyncPublisher,SyncSubscriber,init,spin,spin_once) so users can publish/subscribe without asyncio.GraphContext(auto_start=...)andGraphService.ensure(auto_start=...).Motivation
Many users find
asynciointimidating; the goal is to let them use ezmsg with a simple, synchronous API similar to ROS2 (with ez.sync.init(...),spin(),spin_once()), while preserving ezmsg’s backpressure semantics and zero‑copy safety.This wrapper is explicitly for ergonomics, not peak throughput.
Implementation Details
New Sync API (
src/ezmsg/core/sync.py)GraphContextand runs an asyncio event loop in a background thread usingnew_threaded_event_loop.create_publisher/create_subscriptioncall the underlying async API viaasyncio.run_coroutine_threadsafe.spin()/spin_once()pull messages directly viarecv_zero_copy()and only release backpressure after the user callback returns.spin_once()returns a boolean for “did work”.GraphServer Auto‑Start Control
GraphService.ensure(auto_start: bool | None = None)Nonepreserves existing “auto‑start only when address is not specified + no env override.”True/Falseoverrides.GraphContext(..., auto_start=...)passes through toGraphService.ensure.ez.sync.init(..., auto_start=...)mirrorsGraphContextdefaulting toNone.Examples
examples/simple_publisher.pyexamples/simple_subscriber.pyexamples/simple_async_publisher.pyexamples/simple_async_subscriber.pyTests
tests/test_sync_api.pyPerf Script
tests/perf_sync_overhead.pyThreads and Concurrency Model
spin,spin_once, callbacks).publish()/recv()call crosses threads viarun_coroutine_threadsafe.Performance
Measured using
tests/perf_sync_overhead.py(local macOS example):Interpretation:
Files Changed / Added
src/ezmsg/core/sync.pysrc/ezmsg/core/__init__.pysrc/ezmsg/core/graphcontext.pysrc/ezmsg/core/graphserver.pyexamples/simple_publisher.pyexamples/simple_subscriber.pyexamples/simple_async_publisher.pyexamples/simple_async_subscriber.pytests/test_sync_api.pytests/perf_sync_overhead.pyexamples/lowlevel.py(replaced by split examples)