snu-quiqcl / qiwis

QuIqcl Widget Integration Software
MIT License
5 stars 2 forks source link

Global constant namespace is empty when accessed by other modules #215

Closed kangz12345 closed 9 months ago

kangz12345 commented 9 months ago

Reported by @BECATRUE.

As the original post shows, the global constant namespace becomes empty in other modules. The reason seems to be the importing process when creating the apps. The global constant namespace is initialized in qiwis module, i.e., qiwis.BaseApp. However, the apps defined in other modules import qiwis.BaseApp and it seems the imported BaseApp class obejct and the original BaseApp class object which resides in qiwis module are two distinct class objects, so they do not share their namespace. In other words, the other modules which import qiwis cannot see the changes made in BaseApp class in qiwis module (and vice versa).

Not yet decided how to resolve this.

Original post:

The config.json for test is as follows:

{
    "app": {
        "numgen": {
            "module": "examples.numgen",
            "cls": "NumGenApp",
            "show": true,
            "pos": "left",
            "channel": ["db"],
            "args": {
                "table": "number"
            }
        },
        "dbmgr": {
            "module": "examples.dbmgr",
            "cls": "DBMgrApp",
            "show": true,
            "pos": "right",
            "channel": []
        },
        "poller": {
            "module": "examples.poller",
            "cls": "PollerApp",
            "show": true,
            "pos": "bottom",
            "channel": ["db"],
            "args": {
                "table": "B"
            }
        },
        "logger": {
            "module": "examples.logger",
            "cls": "LoggerApp",
            "show": true,
            "pos": "bottom",
            "channel": []
        }
    },
    "constant": {
        "icon_path": "resources/icon.jpg",
        "background_path": "resources/background.jpg"
    }
}

I printed the following two properties.

Originally posted by @BECATRUE in https://github.com/snu-quiqcl/qiwis/issues/186#issuecomment-1707266627

kangz12345 commented 9 months ago

We observe this strange situation because the two modules are importing each other, i.e., cyclic or circular import. Python uses linear importing system, so there must not exist cyclic import.

Previously, we were doing cyclic import since qiwis imports the app modules just when creating the app object, so there was no cyclic reference that might occur any conflict (unless the app module illegally uses qiwis features to make such conflicts).

However, the global constant namespace introduced a shared variable across the qiwis and app modules, which induces conflicts because of the cyclic imports.

kangz12345 commented 9 months ago

Two resolve this, as @BECATRUE suggested, I will manually set each app's class attribute _constants when instantiate it.