Dynamically load entry points

Bug #1627906 reported by Ali Asgar Adil
22
This bug affects 3 people
Affects Status Importance Assigned to Milestone
stevedore
Confirmed
Undecided
Unassigned

Bug Description

Once the entry points for a namespace have been loaded at the launch of a program, any newly added entry points for the same namespace will not be loaded until the service/program is restarted.

A cache variable is used to load the entry points for a namespace, entry points are only added at first run.

Revision history for this message
Doug Hellmann (doug-hellmann) wrote :

This is working as designed.

What's the use case for doing something different? We could potentially provide an API to clear the cache so an application can trigger a reload.

Changed in python-stevedore:
status: New → Incomplete
Revision history for this message
Jon-Paul Sullivan (jonpaul-sullivan) wrote :

Consider a long running application that may interact with a user and offer them a list of available actions. As more are installed the list of available actions could change based on additional plugins being added to a namespace.

Consider a testing environment, where the pkg_resources.working_set is manipulated to add dummy plugins for testing from memory, to avoid adding unnecessary entry points to the setup.py for the test plugins, and earlier tests initialise stevedore prior to the addition of the fake entries in the working_set.

The docs for entry points also shows use-cases such as loading plugins from an app-specific directory and allowing for such rescanning of available plugins, iirc.

The addition of a flag to loading seems small overhead for a real gain.

Changed in python-stevedore:
status: Incomplete → Confirmed
Revision history for this message
Jon-Paul Sullivan (jonpaul-sullivan) wrote :

As a workaround, you can do the following:

from stevedore import ExtensionManager
ExtensionManager.ENTRY_POINT_CACHE = {}

This drops the cache entirely, and means any plugin load will reinitialise it.

Revision history for this message
belitre (belitre) wrote :

And how do you add a dummy plugin for testing that is implemented in a helper module in tests?

I'm trying to do it with pkg_resources.working_set but after that stevedore.DriveManager does not find the plugin.

    ExtensionManager.ENTRY_POINT_CACHE = {}
    distribution = pkg_resources.Distribution(__file__)

    entry_point = pkg_resources.EntryPoint.parse(
        'plugin_name = tests.plugins.sample:Pluginclass',
        dist=distribution
    )
    distribution._ep_map = {'plugin_ns': {'plugin_name': entry_point}}
    pkg_resources.working_set.add(distribution)
    entry_point.load()

And then:

 driver.DriverManager(
        "plugin_ns",
        "plugin_name"
    )

Revision history for this message
Jon-Paul Sullivan (jonpaul-sullivan) wrote :

I think the python base code just changed and I had to rework my code that I was using for testing. My updated code can be seen here: https://github.com/CiscoDevNet/es-logger/blob/1bd5804e8e2defa706132a188bf1bfc12c9c37ef/test/test_es_logger.py#L31

Effectively the following inserts the dummy plugin into the stevedore cache before I call the application that is using stevedore:

        dummy_ep = importlib.metadata.EntryPoint(
            'dummy', 'test.test_plugins:DummyEventTarget', 'es_logger.plugins.event_target')
        ExtensionManager.ENTRY_POINT_CACHE = {'es_logger.plugins.event_target': [dummy_ep]}

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Duplicates of this bug

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.