Lifetime and change notifications¶
A lookup describes which objects are available. Consumers can retain an object after its registration is removed, and they can retain a result to follow changes in availability. These lifetimes are separate: removing a registration changes future queries through it, while existing references keep their objects alive. Observation has its own lifetime too, because a subscription alone does not keep the result or callback alive.
The lookup model introduces members, items and results. Here, the distinction matters because each can retain a different part of the system.
Registration and retrieval retain different things¶
A direct registration holds its object strongly. A GenericLookup keeps the
direct member through its stored item, while fixed lookups keep their supplied
members for their own lifetime. An object does not disappear from those lookups
merely because the caller drops the reference used to register it.
A deferred registration instead keeps a conversion key and its convertor. Its
item can describe the member before an object exists, and holds only a weak
reference to the converted object once created. If no strong references remain,
that object can be collected while the registration remains available. A later
instance request can then call convert() again. The registration therefore
does not promise exactly-once construction or the same Python object on every
retrieval. Converted products must support weak references.
Weak caching at the item is only part of the retention picture. A generic
result caches its instance collection strongly after all_instances() reads
the members. Holding that result can keep converted objects alive even when
the application has dropped its direct references to them. A matching membership
change clears the generic result's caches, but any collection already retained
by a caller still holds its contents. Collection is possible only after all
strong references disappear, so weak item caching does not imply prompt cleanup.
For example, a report chooser can inspect deferred report descriptions without loading the reports. If it later reads all report instances and retains that collection, it also retains those report objects. Closing the chooser must release its stored collections as well as its result if those references are no longer needed. Removing a report's registration alone cannot release an object the chooser still holds.
This separation is useful when choosing a query. Standard generic item and
class metadata queries can avoid conversion, whereas lookup(),
lookup_expected(), lookup_all() and all_instances() can request objects.
Metadata methods on the convertor can still run during queries and membership
changes. An item can report its advertised type before conversion and its
product's concrete type afterwards. This describes the member's type, rather
than establishing a durable object identity. Item identifiers are defined by
the source or convertor, with no universal uniqueness or persistence guarantee.
Deferred conversion is a specific mechanism
EntryPointLookup loads and constructs all contributions in its group when
it is created, then retains them as fixed members. Reading their metadata
afterwards does not make that construction lazy. See
Entry points and the default lookup for this
distinction, and Defer object creation for
convertor-based membership.
A live result has immutable collection reads¶
A retained dynamic result continues to represent a searched type as membership changes. Each returned collection describes a particular read: an earlier instance tuple does not gain a newly available member or lose a removed one. Its objects remain ordinary mutable Python objects. An item collection also contains descriptions, rather than frozen copies of every metadata value or of the objects those items can supply.
These collection reads are snapshots of membership, while the result is the live view. Retaining both means retaining two different things: the means to read current availability and an earlier collection that may keep old members alive. Composite results hold their source results, and delegated results hold their current source result, so retention can extend through these layers to a source's instance cache. An outer result need not construct its own instance cache to retain objects indirectly.
Observation needs an owner¶
Generic, proxy and delegated lookups track results weakly. A consumer must keep a strong reference to a result for as long as it wants to observe it. Registering a callback on a temporary result leaves no such owner. Once that result is collected, observation ends even if the lookup itself remains alive.
The standard listener registry also holds newly registered callbacks weakly. A typical observer therefore owns its result and remains alive for the duration of observation. Registering its bound method does not keep the observer alive, and a free function or lambda also needs a reference elsewhere if it is to remain subscribed.
Weak references allow abandoned observations to be collected, but explicit unsubscription gives an application a defined end to its interest. Removing the callback when its owner closes prevents further participation while leaving the result available for explicit reads. Proxy and delegated results attach their forwarding callbacks to source results when observation starts and remove them when their last listener goes away. Delegated source switching also moves those subscriptions from the old source to the new one.
The observer's stored collections have a separate lifetime, so unsubscription alone does not release their objects. Applications using an executor must also coordinate pending notification work before shutting down resources, because unsubscription does not wait for submitted work to finish. Observe results shows ownership, initial reads and cleanup in working examples.
A notification signals a possible change¶
A result notification is a reason to refresh the consumer's view of availability. The callback receives the observed result, so a chooser can refresh its list or reconsider an available action by reading that result again. The consumer must read the initial state explicitly because subscription does not deliver it.
This is a refresh signal rather than a membership history. It contains no exact addition, removal or old/new difference, and the API does not promise exactly-once delivery. A delayed read may also include changes made after dispatch began.
Source changes follow the same refresh model. A ProxyLookup combines sources
that can be added or removed. A DelegatedLookup follows one provider-supplied
source and moves retained results when the application explicitly calls
lookup_updated() after changing what its provider supplies. Replacing a source
need not change the visible objects. Source-switch notification checks can inspect
class metadata without deliberately retrieving all instances. These checks do not
calculate an exact difference between two instance collections.
Fixed results expose listener registries for interface consistency, but do not automatically publish membership changes. Changes inside an available object are also separate from changes to its availability. A result observes availability, so applications that need to follow property changes must arrange that observation separately.
Dispatch does not define a concurrency policy¶
The membership owner chooses how generic notifications run. With the default
InstanceContent(), they run synchronously in the mutation call, after affected
generic result caches have been cleared and membership updated. Supplying
InstanceContent(notify_in=executor) instead sends generic notification work
through that executor. Registering a callback does not choose its dispatch mode.
Executor dispatch allows notification work to run separately from the membership change. A later callback reads the result's state at the time of that read, so several changes can be reflected together. Applications own the executor's lifetime and shutdown, and must arrange any UI-thread marshalling they need. Lookups exposes no notification completion handle, so an executor-backed mutation can return while observers still have work to do.
Dispatch choice also leaves coordination decisions to the application:
- Internal locks do not establish general thread safety or atomic reads across several queries or sources. Applications must coordinate concurrent access.
- Synchronous dispatch does not establish a callback ordering or reentrant mutation policy. A generic storage guard can reject mutation while another storage modification is active, without defining every case of nested access.
Completion and success are separate questions even with synchronous dispatch.
The standard dynamic implementations use the Listeners default, which logs and
suppresses ordinary callback Exceptions while allowing other BaseException
subclasses to propagate. Source and provider errors, and failures in direct
conversion or metadata queries, can reach their callers instead. Consequently,
a successful mutation call does not prove that every observer completed
successfully. For practical symptoms and diagnosis, see
Diagnose lookup problems.