Sessions

Please refer to Mesh documentation for a general description of session concept in Mesh.

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

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 automatically send calls to extend the session lifetime in the background.

In the very limited and special use case where you want to connect to an 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 opened via Python’s volue.mesh.Connection.Session object.

Good practices

  • Close unused sessions. If you create the session with a with statement, it is closed automatically when the block exits, even if an exception is raised. Otherwise you should close it explicitly using volue.mesh.Connection.Session.close(). Every open session occupies resources on the Mesh server and runs a background thread in the Python SDK that extends its lifetime, see Timeout above.

  • If possible, reuse the same session for multiple calls to the server, instead of creating a new session for each call. Opening and closing a session requires additional calls to the Mesh server and starting a new lifetime extension thread, so reusing a session reduces this overhead.

  • Commit changes before closing a session. Closing a session discards all uncommitted changes, so call volue.mesh.Connection.Session.commit() for the changes you want to keep. No explicit rollback is needed for changes you want to discard.