Mesh session

A session can be viewed as temporary workspace where changes will not be affected by, or affect other users, that work with the system until changes are committed and pushed out of the session and into where shared resources are stored.

A Mesh server will normally have many separate sessions open at any given time. Using the Python API is just on way of working with sessions. A session should be created and closed after finishing work.

When a session has been created the user can interact with the Mesh model, search for and retrieve data (like time series or information about object) and perform calculations using functions among other things.

If the connection is lost it is possible to reconnect to the server and attach to a open session using a session identifier. If a user does not close a session manually it will time out and will be closed after a specific time, but this might steal resources and should be avoided.

Trying to connect to a session with a session id that is no longer valid will result in an error.

The following example shows some different ways of working with sessions.

import uuid

import helpers

from volue.mesh import Connection


def main(address, tls_root_pem_cert):
    """Showing different ways of working with sessions."""

    # For production environments create connection using: with_tls, with_kerberos, or with_external_access_token, e.g.:
    # connection = Connection.with_tls(address, tls_root_pem_cert)
    connection = Connection.insecure(address)

    # 1. Create a session, open and close session.
    session = connection.create_session()
    session.open()
    print("1. You now have a new open session")
    session.close()

    # 2. Create session using the with statement.
    # Session will be created, opened and closed within the 'with' statement scope
    with connection.create_session() as session:
        print("2. You now have a new open session")

    # 3. Connecting to a potentially open session.
    # Session ids can be found in the session object:
    # session.session_id
    # Note: If the session_id is not the id of an open session,
    # the server will create a new one for you.
    # Set the session id you want to connect to
    session_id = uuid.UUID("123e4567-e89b-12d3-a456-556642440000")
    print(f"3. Session id you want to connect to: {session_id}")
    session = connection.connect_to_session(session_id)
    # Try connecting to that session id, if it does not exist, a new one will be created without warning
    session.open()
    print(
        "3. You have now an open session, either the one you requested or a new one if it did not exist"
    )
    # Check which session you are connected to and close it
    print(f"3. Session id you are to connect to: {session.session_id}")
    session.close()


if __name__ == "__main__":
    address, tls_root_pem_cert = helpers.get_connection_info()
    main(address, tls_root_pem_cert)

Timeout

Each session that has been inactive for some period of time will be automatically closed on the server side. This period is called session timeout. Currently the session timeout for gRPC sessions is set to 5 minutes.

The session timeout is counted from the moment where handling of the last request made using that session was completed. So, if you are using a session for longer period than the session timeout, but you are actively making calls to, for example read time series points, then the session will not timeout.

In cases where a session needs to be preserved, but the inactivity periods are longer, then the user needs to make explicit calls using that session.

To make working with Mesh via Python SDK more user-friendly the extension of session lifetime is handled automatically by the Mesh Python SDK. So as long as you have an opened session, the Python SDK will send automatically calls to extend the session lifetime in the background.

In very limited and special use case where you want to connect to already existing and opened session via volue.mesh.Connection.connect_to_session Python SDK will not automatically extend the lifetime of the session. In such case the user needs to make explicit calls. This is because tracking of an open session that needs automatic lifetime extension is started when it is open via Python’s volue.mesh.Connection.Session object.