KeySessionState
Overview
KeySessionState is an enumeration that represents the possible states of a KeySession during its lifecycle. These states help track the session's progress from initialization through connection establishment to normal operation or error conditions.
States
START
Initial state when the session is created. The session has been instantiated but no connection attempt has been made yet.
REGISTERING
Session is in the process of registering with the KeyAccess server. This involves authenticating the application and establishing initial communication.
REGISTERED
Session has successfully registered with the KeyAccess server. Authentication was successful and the session is authorized to proceed.
REGISTRATION_FAILED
Registration with the KeyAccess server failed. This typically indicates authentication issues such as invalid API keys or unauthorized application names.
CONNECTING
Session is connecting to the gRPC endpoint. The registration was successful and the session is now establishing the main communication channel.
DOWNLOADING_SCHEMA
Session is downloading schema definitions from the platform. This step ensures the client has the latest data type definitions for proper serialization/deserialization.
SCHEMA_DOWNLOAD_FAILED
Failed to download schema definitions. This may indicate network issues or problems with the schema service.
SCHEMA_DOWNLOADED
Schema definitions have been successfully downloaded. The session now has the necessary type information for data operations.
CONNECTED
Session is fully connected and ready for operation. All initialization steps have completed successfully and the session can now perform snapshot, subscribe, and publish operations.
DISCONNECTED
Session is disconnected from the KeyAccess server. This can occur due to network issues, server shutdown, or explicit disconnection via stop() or stop_sync().
ERROR
An error occurred during session operation. This is a general error state that indicates something went wrong that prevented normal operation.
Usage
from keysquarepy import KeySession, KeySessionState
ks = KeySession(
application_name="my-app",
api_key="your-api-key",
url="localhost:6800"
)
# Check current state
current_state = ks.status()
if current_state == KeySessionState.CONNECTED:
print("Ready for data operations")
data = ks.snapshot("Price")
elif current_state == KeySessionState.REGISTRATION_FAILED:
print("Check your API key and application name")
elif current_state == KeySessionState.ERROR:
print("Session encountered an error")
else:
print(f"Session is in state: {current_state}")
State Transitions
The typical state flow for a successful connection is:
START→REGISTERING→REGISTERED→CONNECTING→DOWNLOADING_SCHEMA→SCHEMA_DOWNLOADED→CONNECTED
Possible error paths:
REGISTERING→REGISTRATION_FAILEDDOWNLOADING_SCHEMA→SCHEMA_DOWNLOAD_FAILED- Any state →
ERROR(for unexpected errors) CONNECTED→DISCONNECTED(on normal shutdown or network issues)
See Also
KeySession.status()- Get the current session stateKeySession.start()- Start the session connectionKeySession.stop()- Stop the session connection