Sessions
Session & SessionManager
In subnets that support client access (e.g., for inference or external applications), the Session and SessionManager classes form the foundation of client-to-node communication.
They abstract away routing, retries, and authorization logic — allowing external users or services to interact with the subnet as a consumer.
Session: Stateful Interaction with a Peer Node
Session: Stateful Interaction with a Peer NodeThe Session class represents a single communication session with a remote subnet node, typically a hoster that performs inference or another application-level task.
Key Responsibilities:
Selects a node from the routing manager
Authenticates and connects via the selected protocol (e.g.,
MockProtocol)Calls
rpc_*methods, likerpc_inference_streamHandles retries, failures, and fallback logic
Yields responses asynchronously (e.g., streaming outputs)
Highlights:
session = Session(remote_manager, authorizer)
async for tensor in session.run_protocol_task(prompt, tensor):
# use streamed inference outputAutomatically retries on failure
Uses
RemoteManagerto keep a dynamic list of available nodesAllows custom authorization via
AuthorizerBaseStreams data using
AsyncIterator(e.g., token-by-token inference)
SessionManager: Lightweight Factory
SessionManager: Lightweight FactoryThe SessionManager is a stateless factory that produces Session objects on demand via a context manager.
Why It Exists:
Cleanly manages the lifecycle of a session
Automatically handles session closure (
await session.close())Supports
async withsyntax for resource management
Example Usage:
session_manager = SessionManager(remote_manager, authorizer)
async with session_manager.session() as session:
async for tensor in session.run_protocol_task(prompt, tensor):
# process inference stream🔒 The
SessionManagerdoes not track session history or state across sessions — it simply builds isolatedSessionobjects as needed.
🔐 Built-in Features
Node routing
✅ via RemoteManager
🔁 passed in
Retry logic
✅ automatic retries
–
Auth integration
✅ optional AuthorizerBase
passed down
Streaming support
✅ async tensor stream
–
Resource cleanup
✅ close() method
✅ via context manager
Stateless reuse
–
✅ creates fresh sessions
Summary
Session
A single, stateful interaction with a chosen node
SessionManager
A lightweight factory that spawns new sessions
Together, they allow clients to interact with the subnet in a clean, secure, and fault-tolerant way — ideal for inference requests, chat interfaces, or real-time applications.
Last updated