'How to make a variable global in the module that calls tha function that defines it?
I'm building a simple performance testing framework that will run multiple tests with cProfile concurrently. Unfortunately I can't share my code, but my question is quite general that I don't think it should matter. That being said - here is some context:
The basic structure of my framework is a main object ExperimentManager and Test objects it holds. The ExperimentManager receives different sets of parameters, creates a Test object for each, and when called, it runs the Test objects' run method via ProcessPoolExecutor. Inside each test object, I call cProfile.runctx(cmd, globals(), locals()).
The tests themselves require a separate module to be imported (part of a bigger framework). However, python modules are not picklable so the module can't be passed to each test object as it will be pickled and sent to a new process.
I found that I can sidestep this issue by introducing a "setup" function to the interface. I give this function to the ExperimentManager when initializing it, and it is called inside init. The setup function imports the relevant modules and declares them global. Then, each test receives it through the "globals()" variable since the test objects are in the same module as the experimentManager who just declared them global.
This works well when I write my setup function inside the same module as the testing framework. However, I'd like this framework to be a standalone module that can be imported into another and used to run a test. The issue I'm having is that global variables are global only in the module that defines them. For this reason when the setup function is defined in another module, the one that imports the testing framework, it is only global there and therefore not included in globals() that lives in my framework module.
Another idea I had is to pass the setup function to the test itself, and have it return the module it imports, which the test object will assign to a variable and use. However, this raises an exception that says modules can't be pickled, why would this pickle my module ? I'm only sending thorough the function, the function imports the module in the new process, what am I missing here ?
Let me know if any clarification is needed.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
