Skip to content

Support parent-child state machine communication in Python API #576

@fgmacedo

Description

@fgmacedo

Summary

When using <invoke> via SCXML, parent and child state machines can exchange events using <send target="#_parent"> (child → parent) and <send target="#_<invokeid>"> (parent → child). This communication protocol is not yet available when defining state machines in Python.

Current state

Direction SCXML Python (IInvoke) Python (callable / StateChart)
Parent → Child <send target="#_<invokeid>"> No No
Child → Parent <send target="#_parent"> ctx.send() No
  • IInvoke handlers can send events to the parent via ctx.send(), but plain callables, naming convention methods (on_invoke_<state>), and child StateChart classes have no way to communicate back.
  • There is no public API for the parent to send events to a running child machine.

Gaps

  1. StateChartInvoker lacks on_event()InvokeManager.send_to_child() requires the handler to implement on_event(event, **data), but StateChartInvoker (used for State(invoke=ChildMachine)) doesn't have it.
  2. _invoke_session not set for Python childrenStateChartInvoker.run() instantiates the child without passing _invoke_session, so the child has no reference to the parent. Only SCXMLInvoker sets this up.
  3. No public API on StateChartsend_to_child() exists on InvokeManager but is internal (sm._engine._invoke_manager). There's no user-facing method.
  4. Invoke ID not exposed — users have no way to obtain the invokeid of a running invocation (SCXML uses idlocation= for this).

Possible approach

  • Add on_event() to StateChartInvoker that forwards to self._child.send().
  • Pass _invoke_session when instantiating Python child machines so they can send events to the parent.
  • Expose a public method on StateChart for parent → child communication (e.g., sm.send_to_invoke(invokeid, event, **data)).
  • Consider an autoforward option for State(invoke=..., autoforward=True).

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions