Refinitiv Data Library for Python

session.on_state

This method can be used to register a user defined callback function that is called by the library whenever the state of the session is updated. This callback function allows your application to be notified of any state change and to take actions accordingly.

Callback function signature is:
def callback(state, message, session)

For states, see states

Module

refinitiv.data.session

Syntax

session._on_state(self, callback)

Parameters

Value Description Data type Optional Default value
self session.Session instance itself. Conventional name for the first class method argument in Python. - - -
callback Callback function object. Callable No -

Returned value

None.

Usage

The following example demonstrates how to define the callback function and check the states during the execution of a session.

import refinitiv.data as rd

# create session
DESKTOP_APP_KEY = os.getenv("DESKTOP_APP_KEY")
session = rd.session.desktop.Definition(app_key=DESKTOP_APP_KEY).get_session()
rd.session.set_default(session)


# define callback function.
def check_state(state, message, session):
    # Inside callback function you can do whatever you want using state or message.
    print(f"State: {state}")
    print(f"Message: {message}")
    print("\n")


# define callback
session.on_state(check_state)

# open and close session for demonstration purposes.
session.open()
session.close()

Response

State: OpenState.Pending
Message: Session opening in progress

State: OpenState.Opened
Message: Session is opened

State: OpenState.Closed
Message: Session is closed

None.