liveBook · Manning

Lazy initialization

As explained in Designing AI Systems

Lazy initialization is the creation of an object or resource only when it is first needed.

Lazy initialization creates a service client only when first accessed, then reuses that instance on later accesses. In an SDK, this keeps unused services from creating clients or opening connections. section 2.5.2

A property-based service client follows this sequence:

  • Initialize each client field to None; on property access, import and construct the client if it does not yet exist, then store and return it. section 2.5.2
  • The constructed client connects to the gateway using a secure gRPC channel and identifies its target service in request metadata. section 2.5.2
  • Creating only accessed clients avoids opening channels for unused services, reducing startup time, memory use, network overhead, and connection-pool pressure. section 2.5.2 section 2.6
Listing 2.18 Property-based lazy initialization of service clients
    @property    #A
    def sessions(self):    #A
        if self._sessions is None:    #B
            from .clients.sessions 
➥import SessionClient    #C
            self._sessions = SessionClient(self)    #C
        return self._sessions    #D

The sessions property checks for an existing client, constructs and caches one on first access, then returns it. section 2.5.2

How Designing AI Systems covers it

How deep it goes
Shows fields initialized to None and a property that imports, constructs, caches, and returns a client.
Examples
Uses sessions, models, and data clients, with a property implementation shown for the sessions client.
What you can do after
Apply it via Python properties to create and reuse SDK service clients only when accessed.
How it connects
Connects Python properties and service clients to gRPC channels, API gateways, and request metadata.

Also explained in

Compare how 12 books explain lazy initialization →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Designing AI Systems. The section links point to the parts of the book it draws on.