Client Session

This module is the first place to begin with your Python programs that use Backend.AI API functions.

The high-level API functions cannot be used alone – you must initiate a client session first because each session provides proxy attributes that represent API functions and run on the session itself.

To achieve this, during initialization session objects internally construct new types by combining the BaseFunction class with the attributes in each API function classes, and makes the new types bound to itself. Creating new types every time when creating a new session instance may look weird, but it is the most convenient way to provide class-methods in the API function classes to work with specific session instances.

When designing your application, please note that session objects are intended to live long following the process’ lifecycle, instead of to be created and disposed whenever making API requests.

class ai.backend.client.session.BaseSession(*, config=None, proxy_mode=False)[source]

The base abstract class for sessions.

proxy_mode

If set True, it skips API version negotiation when opening the session.

Return type

bool

abstractmethod open()[source]

Initializes the session and perform version negotiation.

Return type

Optional[Awaitable[None]]

abstractmethod close()[source]

Terminates the session and releases underlying resources.

Return type

Optional[Awaitable[None]]

closed

Checks if the session is closed.

Return type

bool

config

The configuration used by this session object.

Return type

APIConfig

class ai.backend.client.session.Session(*, config=None, proxy_mode=False)[source]

A context manager for API client sessions that makes API requests synchronously. You may call simple request-response APIs like a plain Python function, but cannot use streaming APIs based on WebSocket and Server-Sent Events.

closed

Checks if the session is closed.

Return type

bool

config

The configuration used by this session object.

Return type

APIConfig

proxy_mode

If set True, it skips API version negotiation when opening the session.

Return type

bool

open()[source]

Initializes the session and perform version negotiation.

Return type

None

close()[source]

Terminates the session. It schedules the close() coroutine of the underlying aiohttp session and then enqueues a sentinel object to indicate termination. Then it waits until the worker thread to self-terminate by joining.

Return type

None

worker_thread

The thread that internally executes the asynchronous implementations of the given API functions.

class ai.backend.client.session.AsyncSession(*, config=None, proxy_mode=False)[source]

A context manager for API client sessions that makes API requests asynchronously. You may call all APIs as coroutines. WebSocket-based APIs and SSE-based APIs returns special response types.

closed

Checks if the session is closed.

Return type

bool

config

The configuration used by this session object.

Return type

APIConfig

proxy_mode

If set True, it skips API version negotiation when opening the session.

Return type

bool

open()[source]

Initializes the session and perform version negotiation.

Return type

Awaitable[None]

close()[source]

Terminates the session and releases underlying resources.

Return type

Awaitable[None]