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_stream
Handles 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 output
✅ Automatically retries on failure
✅ Uses
RemoteManager
to keep a dynamic list of available nodes✅ Allows custom authorization via
AuthorizerBase
✅ Streams 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 with
syntax 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
SessionManager
does not track session history or state across sessions — it simply builds isolatedSession
objects 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