This forum is now a read-only archive. All commenting, posting, registration services have been turned off. Those needing community support and/or wanting to ask questions should refer to the Tag/Forum map, and to http://spring.io/questions for a curated list of stackoverflow tags that Pivotal engineers, and the community, monitor.

AnnouncementAnnouncement Module

Collapse

No announcement yet.

How to access application context from another python module?Page Title Module

Okay, I see the problem. It is one of the gotcha's when you use Python's class-level variables. I thought this might be the issue. When you refer to global_app_context inside the set_app_context method, you need to prefix it with the class name, or it will treat it like a local variable.

Comment

Did you try to use ApplicationContextProvider inside another module, (ex. test.py)? Hope prefixing the variable with a class name won't raise NoneType error, when trying to get global_app_context (ApplicationContextProvider.global_app_context).

Comment

Well, your solution is working because you are calling "testit()" method from the same module where container is being created. I want to have global access to application context from any point of my app, after main module had been executed. Is that possible?

Comment

I'm developing a messaging system based on ZeroMQ, so it's no about web application. In the main module where the container is being instanced, I get various objects from container, message listener, xml-rpc server, etc, and then I initialize them. From another module, call it test.py, I need to get another object from the container (message producer) which will send the message each time the module is executed.
Initially I had a global variable (container) inside my main module, but that wasn't a good idea, because each time I execute my test.py module, and reference the global variable, the application context had been reloaded.
What I need is some kind of static reference to my message producer once the container bootstrapping is completed, so I can use it module widely.

There is a possibility that in that architecture, the bootstrapped container and your separate module are running in either separate threads or even separate instances of Python, in which case, you would not be getting what is expected. To tackle that, perhaps if you exported ApplicationContextProvider using PyroServiceExporter, then your client can access it using PyroProxyFactory. You might need a wrapper method to serve up the property, because I don't think properties are exported (yet). Check out http://static.springsource.org/sprin...remote-objects.

Comment

Okay, My spouse and i see the problem. It happens to be among the list of gotcha's usuallu when you use Python's class-level variables. I was thinking this may be the issue. When you refer to global_app_context from the set_app_context method, it is advisable to prefix it together with the class name, or it will eventually treat it like a local variable.http://houlland.livejournal.com/799.html