pywry.state¶
State management interfaces and implementations.
Factory Functions¶
pywry.state.get_widget_store
cached
¶
get_widget_store() -> WidgetStore
Get the configured widget store instance.
Uses Redis backend in deploy mode if configured, otherwise memory.
| RETURNS | DESCRIPTION |
|---|---|
WidgetStore
|
The widget store instance. |
pywry.state.get_event_bus
cached
¶
get_event_bus() -> EventBus
Get the configured event bus instance.
Uses Redis Pub/Sub in deploy mode if configured, otherwise memory.
| RETURNS | DESCRIPTION |
|---|---|
EventBus
|
The event bus instance. |
pywry.state.get_connection_router
cached
¶
get_connection_router() -> ConnectionRouter
Get the configured connection router instance.
Uses Redis in deploy mode if configured, otherwise memory.
| RETURNS | DESCRIPTION |
|---|---|
ConnectionRouter
|
The connection router instance. |
pywry.state.get_session_store
cached
¶
get_session_store() -> SessionStore
Get the configured session store instance.
Uses Redis in deploy mode if configured, otherwise memory.
| RETURNS | DESCRIPTION |
|---|---|
SessionStore
|
The session store instance. |
pywry.state.get_callback_registry
¶
get_callback_registry() -> CallbackRegistry
Get the global callback registry instance.
| RETURNS | DESCRIPTION |
|---|---|
CallbackRegistry
|
The singleton callback registry. |
pywry.state.get_worker_id
¶
Get the unique worker ID for this process.
The worker ID is used for connection routing and callback dispatch in multi-worker deployments.
| RETURNS | DESCRIPTION |
|---|---|
str
|
Unique worker identifier. |
pywry.state.get_state_backend
¶
get_state_backend() -> StateBackend
Get the configured state backend.
| RETURNS | DESCRIPTION |
|---|---|
StateBackend
|
The configured backend (MEMORY or REDIS). |
pywry.state.is_deploy_mode
¶
Check if running in deploy mode.
Deploy mode enables: - Redis state backend (if configured) - Multi-worker support - External state storage - Session/RBAC support
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if running in deploy mode. |
pywry.state.clear_state_caches
¶
Clear all cached state store instances.
Call this to force re-creation of stores (e.g., after config change).
Abstract Interfaces¶
pywry.state.WidgetStore
¶
Bases: ABC
Abstract widget storage interface.
Handles storage and retrieval of widget HTML content and metadata. Implementations must be thread-safe and support async operations.
Functions¶
register
abstractmethod
async
¶
register(widget_id: str, html: str, token: str | None = None, owner_worker_id: str | None = None, metadata: dict[str, Any] | None = None) -> None
Register a widget with its HTML content.
| PARAMETER | DESCRIPTION |
|---|---|
widget_id
|
Unique identifier for the widget.
TYPE:
|
html
|
The HTML content of the widget.
TYPE:
|
token
|
Optional per-widget authentication token.
TYPE:
|
owner_worker_id
|
ID of the worker that created this widget.
TYPE:
|
metadata
|
Additional metadata (title, theme, etc.).
TYPE:
|
get
abstractmethod
async
¶
get(widget_id: str) -> WidgetData | None
Get complete widget data.
| PARAMETER | DESCRIPTION |
|---|---|
widget_id
|
The widget ID to retrieve.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
WidgetData or None
|
The widget data if found, None otherwise. |
get_html
abstractmethod
async
¶
Get widget HTML content.
| PARAMETER | DESCRIPTION |
|---|---|
widget_id
|
The widget ID.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str or None
|
The HTML content if found, None otherwise. |
get_token
abstractmethod
async
¶
Get widget authentication token.
| PARAMETER | DESCRIPTION |
|---|---|
widget_id
|
The widget ID.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str or None
|
The token if set, None otherwise. |
exists
abstractmethod
async
¶
Check if a widget exists.
| PARAMETER | DESCRIPTION |
|---|---|
widget_id
|
The widget ID.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if the widget exists. |
delete
abstractmethod
async
¶
Delete a widget.
| PARAMETER | DESCRIPTION |
|---|---|
widget_id
|
The widget ID to delete.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if the widget was deleted, False if it didn't exist. |
list_active
abstractmethod
async
¶
List all active widget IDs.
| RETURNS | DESCRIPTION |
|---|---|
list[str]
|
List of active widget IDs. |
update_html
abstractmethod
async
¶
Update widget HTML content.
| PARAMETER | DESCRIPTION |
|---|---|
widget_id
|
The widget ID.
TYPE:
|
html
|
The new HTML content.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if updated, False if widget doesn't exist. |
update_token
abstractmethod
async
¶
Update widget authentication token.
| PARAMETER | DESCRIPTION |
|---|---|
widget_id
|
The widget ID.
TYPE:
|
token
|
The new authentication token.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if updated, False if widget doesn't exist. |
count
abstractmethod
async
¶
Get the number of active widgets.
| RETURNS | DESCRIPTION |
|---|---|
int
|
Number of active widgets. |
pywry.state.EventBus
¶
Bases: ABC
Abstract event publishing interface.
Handles cross-worker event delivery for callback dispatch and real-time updates.
Functions¶
publish
abstractmethod
async
¶
publish(channel: str, event: EventMessage) -> None
Publish an event to a channel.
| PARAMETER | DESCRIPTION |
|---|---|
channel
|
The channel name (e.g., "widget:{id}", "worker:{id}").
TYPE:
|
event
|
The event to publish.
TYPE:
|
subscribe
abstractmethod
async
¶
subscribe(channel: str) -> AsyncIterator[EventMessage]
Subscribe to events on a channel.
| PARAMETER | DESCRIPTION |
|---|---|
channel
|
The channel name.
TYPE:
|
| YIELDS | DESCRIPTION |
|---|---|
EventMessage
|
Events received on the channel. |
unsubscribe
abstractmethod
async
¶
Unsubscribe from a channel.
| PARAMETER | DESCRIPTION |
|---|---|
channel
|
The channel to unsubscribe from.
TYPE:
|
pywry.state.ConnectionRouter
¶
Bases: ABC
Abstract connection routing interface.
Tracks which worker owns which WebSocket connection, enabling cross-worker message routing.
Functions¶
register_connection
abstractmethod
async
¶
register_connection(widget_id: str, worker_id: str, user_id: str | None = None, session_id: str | None = None) -> None
Register that a widget is connected to a specific worker.
| PARAMETER | DESCRIPTION |
|---|---|
widget_id
|
The widget ID.
TYPE:
|
worker_id
|
The worker ID that owns this connection.
TYPE:
|
user_id
|
Optional user ID for RBAC.
TYPE:
|
session_id
|
Optional session ID for tracking.
TYPE:
|
get_connection_info
abstractmethod
async
¶
get_connection_info(widget_id: str) -> ConnectionInfo | None
Get connection information for a widget.
| PARAMETER | DESCRIPTION |
|---|---|
widget_id
|
The widget ID.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
ConnectionInfo or None
|
Connection information if connected, None otherwise. |
get_owner
abstractmethod
async
¶
Get the worker ID that owns this widget's connection.
| PARAMETER | DESCRIPTION |
|---|---|
widget_id
|
The widget ID.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str or None
|
The worker ID if connected, None otherwise. |
refresh_heartbeat
abstractmethod
async
¶
Refresh the heartbeat timestamp for a connection.
| PARAMETER | DESCRIPTION |
|---|---|
widget_id
|
The widget ID.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if refreshed, False if connection doesn't exist. |
unregister_connection
abstractmethod
async
¶
Unregister a connection.
| PARAMETER | DESCRIPTION |
|---|---|
widget_id
|
The widget ID.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if unregistered, False if didn't exist. |
list_worker_connections
abstractmethod
async
¶
List all widget IDs connected to a specific worker.
| PARAMETER | DESCRIPTION |
|---|---|
worker_id
|
The worker ID.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[str]
|
List of widget IDs connected to this worker. |
pywry.state.SessionStore
¶
Bases: ABC
Abstract session storage interface for RBAC support.
Handles user sessions and access control for multi-tenant deployments.
Functions¶
create_session
abstractmethod
async
¶
create_session(session_id: str, user_id: str, roles: list[str] | None = None, ttl: int | None = None, metadata: dict[str, Any] | None = None) -> UserSession
Create a new user session.
| PARAMETER | DESCRIPTION |
|---|---|
session_id
|
Unique session identifier.
TYPE:
|
user_id
|
User identifier.
TYPE:
|
roles
|
User roles for access control.
TYPE:
|
ttl
|
Time-to-live in seconds (None for no expiry).
TYPE:
|
metadata
|
Additional session metadata.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
UserSession
|
The created session. |
get_session
abstractmethod
async
¶
get_session(session_id: str) -> UserSession | None
Get a session by ID.
| PARAMETER | DESCRIPTION |
|---|---|
session_id
|
The session ID.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
UserSession or None
|
The session if found and not expired, None otherwise. |
validate_session
abstractmethod
async
¶
Validate a session is active and not expired.
| PARAMETER | DESCRIPTION |
|---|---|
session_id
|
The session ID.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if the session is valid. |
delete_session
abstractmethod
async
¶
Delete a session.
| PARAMETER | DESCRIPTION |
|---|---|
session_id
|
The session ID.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if deleted, False if didn't exist. |
refresh_session
abstractmethod
async
¶
Refresh a session's expiry time.
| PARAMETER | DESCRIPTION |
|---|---|
session_id
|
The session ID.
TYPE:
|
extend_ttl
|
New TTL in seconds (None to use original TTL).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if refreshed, False if session doesn't exist. |
list_user_sessions
abstractmethod
async
¶
list_user_sessions(user_id: str) -> list[UserSession]
List all sessions for a user.
| PARAMETER | DESCRIPTION |
|---|---|
user_id
|
The user ID.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[UserSession]
|
List of active sessions for this user. |
check_permission
abstractmethod
async
¶
Check if a session has permission to access a resource.
| PARAMETER | DESCRIPTION |
|---|---|
session_id
|
The session ID.
TYPE:
|
resource_type
|
The type of resource (e.g., "widget", "user").
TYPE:
|
resource_id
|
The resource identifier.
TYPE:
|
permission
|
The required permission (e.g., "read", "write", "admin").
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if the session has the required permission. |
Data Types¶
pywry.state.WidgetData
dataclass
¶
WidgetData(widget_id: str, html: str, token: str | None = None, created_at: float = 0.0, owner_worker_id: str | None = None, metadata: dict[str, Any] = dict())
Widget data stored in the state store.
| ATTRIBUTE | DESCRIPTION |
|---|---|
widget_id |
Unique identifier for the widget.
TYPE:
|
html |
The HTML content of the widget.
TYPE:
|
token |
Optional per-widget authentication token.
TYPE:
|
created_at |
Unix timestamp when the widget was created.
TYPE:
|
owner_worker_id |
ID of the worker that created/owns this widget's callbacks.
TYPE:
|
metadata |
Additional metadata (title, theme, etc.).
TYPE:
|
pywry.state.EventMessage
dataclass
¶
EventMessage(event_type: str, widget_id: str, data: dict[str, Any], source_worker_id: str, target_worker_id: str | None = None, timestamp: float = 0.0, message_id: str = '')
Event message for cross-worker communication.
| ATTRIBUTE | DESCRIPTION |
|---|---|
event_type |
The type of event (e.g., "click", "cellValueChanged").
TYPE:
|
widget_id |
The target widget ID.
TYPE:
|
data |
The event payload.
TYPE:
|
source_worker_id |
The worker that sent this event.
TYPE:
|
target_worker_id |
The specific worker to receive this event (None for broadcast).
TYPE:
|
timestamp |
Unix timestamp when the event was created.
TYPE:
|
message_id |
Unique identifier for this message.
TYPE:
|
pywry.state.ConnectionInfo
dataclass
¶
ConnectionInfo(widget_id: str, worker_id: str, connected_at: float = 0.0, last_heartbeat: float = 0.0, user_id: str | None = None, session_id: str | None = None)
Information about a WebSocket connection.
| ATTRIBUTE | DESCRIPTION |
|---|---|
widget_id |
The widget this connection is for.
TYPE:
|
worker_id |
The worker ID that owns this connection.
TYPE:
|
connected_at |
Unix timestamp when the connection was established.
TYPE:
|
last_heartbeat |
Unix timestamp of the last heartbeat/activity.
TYPE:
|
user_id |
Optional user ID for RBAC.
TYPE:
|
session_id |
Optional session ID for tracking.
TYPE:
|
pywry.state.UserSession
dataclass
¶
UserSession(session_id: str, user_id: str, roles: list[str] = list(), created_at: float = 0.0, expires_at: float | None = None, metadata: dict[str, Any] = dict())
User session information for RBAC support.
| ATTRIBUTE | DESCRIPTION |
|---|---|
session_id |
Unique session identifier.
TYPE:
|
user_id |
User identifier.
TYPE:
|
roles |
User roles for access control.
TYPE:
|
created_at |
Unix timestamp when the session was created.
TYPE:
|
expires_at |
Unix timestamp when the session expires (None for no expiry).
TYPE:
|
metadata |
Additional session metadata.
TYPE:
|
pywry.state.StateBackend
¶
Bases: str, Enum
Available state storage backends.
Callback Registry¶
pywry.state.CallbackRegistry
¶
In-process callback registry.
Callbacks cannot be serialized to Redis, so they remain local to each worker. The registry tracks which callbacks are registered per widget and event type.
Initialize the callback registry.
Functions¶
register
async
¶
Register a callback for a widget event.
| PARAMETER | DESCRIPTION |
|---|---|
widget_id
|
The widget ID.
TYPE:
|
event_type
|
The event type (e.g., "click", "cellValueChanged").
TYPE:
|
callback
|
The callback function to execute.
TYPE:
|
get
async
¶
get(widget_id: str, event_type: str) -> CallbackRegistration | None
Get a callback registration.
| PARAMETER | DESCRIPTION |
|---|---|
widget_id
|
The widget ID.
TYPE:
|
event_type
|
The event type.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
CallbackRegistration or None
|
The registration if found. |
has_widget
async
¶
Check if any callbacks are registered for a widget.
| PARAMETER | DESCRIPTION |
|---|---|
widget_id
|
The widget ID.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if callbacks exist for this widget. |
has_callback
async
¶
Check if a specific callback is registered.
| PARAMETER | DESCRIPTION |
|---|---|
widget_id
|
The widget ID.
TYPE:
|
event_type
|
The event type.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if the callback exists. |
invoke
async
¶
Invoke a callback if it exists locally.
| PARAMETER | DESCRIPTION |
|---|---|
widget_id
|
The widget ID.
TYPE:
|
event_type
|
The event type.
TYPE:
|
data
|
The event data to pass to the callback.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[bool, Any]
|
(success, result) - success is True if callback was found and executed. |
unregister
async
¶
Unregister a specific callback.
| PARAMETER | DESCRIPTION |
|---|---|
widget_id
|
The widget ID.
TYPE:
|
event_type
|
The event type.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if the callback was removed. |
unregister_widget
async
¶
Unregister all callbacks for a widget.
| PARAMETER | DESCRIPTION |
|---|---|
widget_id
|
The widget ID.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
int
|
Number of callbacks removed. |
list_widget_events
async
¶
List all event types registered for a widget.
| PARAMETER | DESCRIPTION |
|---|---|
widget_id
|
The widget ID.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[str]
|
List of event types. |
list_widgets
async
¶
List all widget IDs with registered callbacks.
| RETURNS | DESCRIPTION |
|---|---|
list[str]
|
List of widget IDs. |
get_stats
async
¶
Get registry statistics.
| RETURNS | DESCRIPTION |
|---|---|
dict
|
Statistics about registered callbacks. |
pywry.state.reset_callback_registry
¶
Reset the global callback registry (for testing).
Memory Implementations¶
pywry.state.MemoryWidgetStore
¶
Bases: WidgetStore
In-memory widget store for single-process deployments.
Thread-safe implementation using asyncio locks.
Initialize the memory widget store.
pywry.state.MemoryEventBus
¶
Bases: EventBus
In-memory event bus for single-process deployments.
Uses asyncio.Queue for inter-task communication.
Initialize the memory event bus.
Functions¶
subscribe
async
¶
subscribe(channel: str) -> AsyncIterator[EventMessage]
Subscribe to events on a channel.
unsubscribe
async
¶
Unsubscribe from a channel (clears all subscriptions).
pywry.state.MemoryConnectionRouter
¶
Bases: ConnectionRouter
In-memory connection router for single-process deployments.
Initialize the memory connection router.
Functions¶
register_connection
async
¶
register_connection(widget_id: str, worker_id: str, user_id: str | None = None, session_id: str | None = None) -> None
Register that a widget is connected to a specific worker.
get_connection_info
async
¶
get_connection_info(widget_id: str) -> ConnectionInfo | None
Get connection information for a widget.
get_owner
async
¶
Get the worker ID that owns this widget's connection.
refresh_heartbeat
async
¶
Refresh the heartbeat timestamp for a connection.
unregister_connection
async
¶
Unregister a connection.
list_worker_connections
async
¶
List all widget IDs connected to a specific worker.
pywry.state.MemorySessionStore
¶
Bases: SessionStore
In-memory session store for RBAC support.
For single-process deployments and development.
Initialize the memory session store.
Functions¶
create_session
async
¶
create_session(session_id: str, user_id: str, roles: list[str] | None = None, ttl: int | None = None, metadata: dict[str, Any] | None = None) -> UserSession
Create a new user session.
validate_session
async
¶
Validate a session is active and not expired.
refresh_session
async
¶
Refresh a session's expiry time.
list_user_sessions
async
¶
list_user_sessions(user_id: str) -> list[UserSession]
List all sessions for a user.
check_permission
async
¶
Check if a session has permission to access a resource.
Currently implements simple role-based access control. Resource-specific permissions can be added via metadata.
set_role_permissions
¶
Configure permissions for a role (sync for setup).
Server State¶
pywry.state.ServerStateManager
¶
Unified state manager for PyWry server.
Automatically selects the appropriate storage backend based on deploy mode configuration. Provides both sync and async interfaces where appropriate.
| ATTRIBUTE | DESCRIPTION |
|---|---|
deploy_mode |
True if running in horizontally scaled deploy mode.
TYPE:
|
worker_id |
Unique identifier for this worker process.
TYPE:
|
Initialize the state manager.
Attributes¶
widgets
property
¶
Get local widgets dict.
Note: In deploy mode, this only returns widgets that were created by this worker and may not reflect the full state. Use async methods for accurate state in deploy mode.
Functions¶
register_widget
async
¶
register_widget(widget_id: str, html: str, token: str | None = None, metadata: dict[str, Any] | None = None) -> None
Register a widget with its HTML content.
In deploy mode, this stores in Redis. In local mode, stores in-memory.
| PARAMETER | DESCRIPTION |
|---|---|
widget_id
|
Unique widget identifier.
TYPE:
|
html
|
The widget's HTML content.
TYPE:
|
token
|
Optional per-widget authentication token.
TYPE:
|
metadata
|
Additional metadata (title, theme, etc.).
TYPE:
|
get_widget
async
¶
get_widget(widget_id: str) -> WidgetData | None
Get widget data by ID.
| PARAMETER | DESCRIPTION |
|---|---|
widget_id
|
The widget ID to retrieve.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
WidgetData | None
|
Widget data if found. |
get_widget_html
async
¶
Get widget HTML content.
| PARAMETER | DESCRIPTION |
|---|---|
widget_id
|
The widget ID.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str | None
|
The HTML content if widget exists. |
update_widget_html
async
¶
Update widget HTML content.
| PARAMETER | DESCRIPTION |
|---|---|
widget_id
|
The widget ID.
TYPE:
|
html
|
New HTML content.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if update succeeded. |
widget_exists
async
¶
Check if a widget exists.
| PARAMETER | DESCRIPTION |
|---|---|
widget_id
|
The widget ID.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if widget exists. |
remove_widget
async
¶
Remove a widget.
| PARAMETER | DESCRIPTION |
|---|---|
widget_id
|
The widget ID.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if widget was removed. |
list_widgets
async
¶
List all widget IDs.
| RETURNS | DESCRIPTION |
|---|---|
list[str]
|
List of widget IDs. |
register_connection
async
¶
Register a WebSocket connection for a widget.
| PARAMETER | DESCRIPTION |
|---|---|
widget_id
|
The widget ID.
TYPE:
|
websocket
|
The WebSocket connection.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Queue
|
Event queue for this connection. |
unregister_connection
async
¶
Unregister a WebSocket connection.
| PARAMETER | DESCRIPTION |
|---|---|
widget_id
|
The widget ID.
TYPE:
|
get_connection
async
¶
Get the WebSocket connection for a widget (local only).
| PARAMETER | DESCRIPTION |
|---|---|
widget_id
|
The widget ID.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
WebSocket | None
|
The connection if it exists on this worker. |
get_event_queue
async
¶
Get the event queue for a widget.
| PARAMETER | DESCRIPTION |
|---|---|
widget_id
|
The widget ID.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Queue | None
|
The event queue if widget is connected on this worker. |
register_callback
async
¶
Register a callback for widget events.
| PARAMETER | DESCRIPTION |
|---|---|
widget_id
|
The widget ID.
TYPE:
|
event_type
|
The event type to handle.
TYPE:
|
callback
|
The callback function.
TYPE:
|
get_callback
async
¶
Get a callback for a widget event.
| PARAMETER | DESCRIPTION |
|---|---|
widget_id
|
The widget ID.
TYPE:
|
event_type
|
The event type.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
callable | None
|
The callback if registered. |
invoke_callback
async
¶
Invoke a callback for a widget event.
| PARAMETER | DESCRIPTION |
|---|---|
widget_id
|
The widget ID.
TYPE:
|
event_type
|
The event type.
TYPE:
|
data
|
Event data.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[bool, Any]
|
(success, result) tuple. |
broadcast_event
async
¶
Broadcast an event to a widget.
In deploy mode, uses Redis Pub/Sub to reach the correct worker. In local mode, sends directly to the local event queue.
| PARAMETER | DESCRIPTION |
|---|---|
widget_id
|
The widget ID.
TYPE:
|
event_type
|
The event type.
TYPE:
|
data
|
Event data.
TYPE:
|
send_to_widget
async
¶
Send an event to a specific widget's WebSocket.
| PARAMETER | DESCRIPTION |
|---|---|
widget_id
|
The widget ID.
TYPE:
|
event
|
The event to send.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if event was queued for sending. |
create_session
async
¶
create_session(user_id: str, roles: list[str] | None = None, metadata: dict[str, Any] | None = None) -> str
Create a new user session.
| PARAMETER | DESCRIPTION |
|---|---|
user_id
|
The user identifier.
TYPE:
|
roles
|
User roles for RBAC.
TYPE:
|
metadata
|
Additional session metadata.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
The session ID. |
get_session
async
¶
Get a session by ID.
| PARAMETER | DESCRIPTION |
|---|---|
session_id
|
The session ID.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
UserSession | None
|
The session if found. |
pywry.state.get_server_state
¶
get_server_state() -> ServerStateManager
Get the global server state manager.
| RETURNS | DESCRIPTION |
|---|---|
ServerStateManager
|
The singleton state manager instance. |
pywry.state.reset_server_state
¶
Reset the server state manager (for testing).
Sync Helpers¶
pywry.state.run_async
¶
Run an async coroutine from sync code.
Uses the server's event loop if available, otherwise creates a temporary one.
NOTE: This function CANNOT be called from within an async context on the
server loop - it will deadlock. Use await directly in async code.
| PARAMETER | DESCRIPTION |
|---|---|
coro
|
The coroutine to run.
TYPE:
|
timeout
|
Timeout in seconds. Default is 5.0.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
T
|
The result of the coroutine. |
| RAISES | DESCRIPTION |
|---|---|
TimeoutError
|
If the operation times out. |
RuntimeError
|
If called from within the server's event loop (would deadlock). |
pywry.state.run_async_fire_and_forget
¶
Schedule an async coroutine without waiting for result.
| PARAMETER | DESCRIPTION |
|---|---|
coro
|
The coroutine to run.
TYPE:
|
Additional Types¶
pywry.state.callbacks.CallbackRegistration
dataclass
¶
CallbackRegistration(widget_id: str, event_type: str, callback: CallbackFunc | AsyncCallbackFunc, is_async: bool = False, created_at: float = time(), invoke_count: int = 0, last_invoked: float | None = None)
Memory Store Factory¶
pywry.state.memory.create_memory_stores
¶
create_memory_stores() -> tuple[MemoryWidgetStore, MemoryEventBus, MemoryConnectionRouter, MemorySessionStore]
Create all in-memory state stores.
| RETURNS | DESCRIPTION |
|---|---|
tuple
|
(widget_store, event_bus, connection_router, session_store) |