// Documentation
Python SDK reference.
cosmonapse Python package - verified against packages/python-sdk. If something here disagrees with the code, the code wins.
SDK · 07
Lifecycle hooks
Both Axon and Dendrite mix in LifecycleHooks. The same three decorators are available on each.
| Hook | Fires when |
|---|---|
| on_connect(fn) | Once, after this component finishes its own connect handshake. For an Axon, after the hosting Dendrite emits REGISTER; for a Dendrite, after synapse subscriptions are wired. |
| on_refresh(fn) | On each refresh event - a heartbeat tick, a REGISTER / DEREGISTER / HEARTBEAT observed via the registry mirror, or a manual await component.refresh(reason=...). The handler receives a RefreshEvent(reason, neuron_id, extra). |
| on_schedule(every_s=N)(fn) | Developer-supplied periodic task. Runs as a background coroutine every every_s seconds for the lifetime of the component. |
async method
component.refresh(*, reason='manual', neuron_id=None, extra=None) -> NoneManually fire on_refresh handlers with the supplied RefreshEvent. Use when your own code knows state has changed but the SDK can't detect it.
hooks.py
# 1. Announce on connect @dendrite.on_connect async def hello(d): snap = await d.registry_snapshot() log.info(f"Up on {d.namespace}: {len(snap)} known neurons") # 2. React to refresh events (heartbeat / register / deregister / manual) @dendrite.on_refresh async def reconcile(d, event): if event.reason == "register": log.info("New peer: %s", event.neuron_id) # 3. Periodic background task @dendrite.on_schedule(every_s=30) async def rollup(d): snap = await d.registry_snapshot(include_deregistered=True) publish_derived_view(snap) # 4. Manual trigger await dendrite.refresh(reason="manual", extra={"why": "side-channel update"})
Have a feature in mind?
The protocol, SDKs, and CLI are still pre-1.0. If something here is missing, ambiguous, or wrong - open an issue and propose a change. Every breaking change is debated in DECISIONS.md first.