Skip to content

Stream inputs and outputs

Create a streaming session when input arrives over time or one input produces several outputs. The application owns the session and closes it when finished.

Run the example

After installation, run:

python examples/python/streaming_echo.py

The complete example reads the echo manifest, sends two text inputs concurrently with receiving, signals completion, and closes in finally. Use the local environment's Python. It verifies the graph's two expected echoes: data hello and data again.

"""Send two inputs, receive their two echoes across output kinds, and close.

This graph has a known one-output-per-input contract. Input completion alone
does not establish that the installed runtime will close every output channel.
"""

import asyncio
from pathlib import Path

import remotemedia.runtime as rt


async def send_inputs(session):
    for text in ("hello", "again"):
        await session.send_input(text)
    await session.signal_input_complete()


async def receive_outputs(session):
    received = []
    for _ in range(2):
        output = await session.recv_output()
        if output is None:
            raise RuntimeError("Echo session ended before both outputs arrived")
        kind, data = output
        print(kind, data)
        received.append((kind, data))
    assert received == [("data", "hello"), ("data", "again")]


async def exchange(session):
    async with asyncio.TaskGroup() as tasks:
        tasks.create_task(send_inputs(session))
        tasks.create_task(receive_outputs(session))


async def main():
    manifest = Path(__file__).with_name("echo.json").read_text(encoding="utf-8")
    session = await asyncio.wait_for(rt.create_streaming_session(manifest), timeout=10)
    try:
        await asyncio.wait_for(exchange(session), timeout=10)
    finally:
        await session.close()


if __name__ == "__main__":
    asyncio.run(main())

Receive every output kind

The native surface takes a JSON manifest string:

import remotemedia.runtime as rt

async def echo_stream(manifest_json):
    session = await rt.create_streaming_session(manifest_json)
    try:
        await session.send_input("hello")
        await session.signal_input_complete()
        output = await session.recv_output()
        if output is None:
            raise RuntimeError("Echo session ended without its expected output")
        kind, data = output
        print(kind, data)
    finally:
        await session.close()

This short example sends one item. For sustained input, use the producer/consumer structure in the complete example so an output queue cannot block a producer that is waiting to finish all its sends.

recv_output() returns (kind, data), with kind equal to audio, video, or data; it returns None after all output-kind channels close. The kind-specific methods are recv_audio(), recv_video(), and recv_data(). Choose one consumption strategy; competing readers consume from the same queues.

Complete and close

signal_input_complete() closes input while allowing output to drain. Later send_input() calls fail. close() tears down the session and is idempotent. Closing immediately after the last send can discard output you intended to receive, so consume the expected results first for finite work.

In the tested local profile, both echoes arrive but input completion alone does not produce observed EOF. The initial ten-second drain check timed out; the repeat lifecycle probe also timed out. Explicit close() followed by recv_output() returns None. The example uses the echo graph's known output count. For variable-output graphs, define an application completion event or stop condition and verify it on the deployed runtime.

Boundary checked Observed result
Send after input completion RuntimeError: input channel closed
Timeout awaiting an empty output queue TimeoutError; explicit close succeeds
Consumer exits with unread output Close succeeds; unread output is discarded
Close twice Both calls succeed
Receive after close None

Run python scripts/verify_examples.py to repeat these checks. Each fresh process must exit without forced child cleanup. After a cancelled receive, close the session; resuming consumption after that cancellation is unverified. close() may wait on receiver locks, so cancel/finish application receive tasks before closing.

A graph with an ongoing source may not finish simply because external input ended. Use an application deadline or stop condition and close the session. Cancelling generation inside a node is a separate operation from ending session input.

Attribute and inspect output

recv_event() returns output with participant information. create_shared_streaming_session() lets local participants join a shared key; each handle leaves on close, and the last handle shuts the shared session down. Dedicated shared-session examples are planned.

Session input_copy_report() and node_copy_reports() expose different accounting scopes. Buffer policies and reports need their own guide; their presence does not establish zero-copy across an entire graph or network.

Continue with session control to inspect a specific node or inject data at a named input.