I am trying to use pybind11 to write python binding for an existing C++ project. It generates a '.so' file which can be import
ed in python.
The problem is that the C++ code uses a lot of global variables for states. So if I want to have 2 seperate instances, I need to somehow import the module twice.
So far, the following methods do not work, assuming the file is my_module.so
- naive method
import my_module as m1
import my_module as m2
assert m1 is not m2 # fail!
- method from another post
def load():
spec = importlib.util.find_spec('my_module')
m = importlib.util.module_from_spec(spec)
spec.loader.exec_module(m)
return m
m1 = load()
m2 = load()
assert m1 is not m2 # fail!