0

I am trying to use pybind11 to write python binding for an existing C++ project. It generates a '.so' file which can be imported 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

  1. naive method
import my_module as m1
import my_module as m2
assert m1 is not m2 # fail!
  1. 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!
John Ao
  • 89
  • 7

1 Answers1

0

For anyone who has the same problem, I come up with a dirty but working solution.

If you want to have n copies, then you have to make n copies of the .so file, say my_module_*.so.

Then use the following code to import them.

import importlib

modules=[]
for i in range(n):
    loader = importlib.machinery.ExtensionFileLoader('my_module', f'/path/to/my_module_{i}.so')
    spec = importlib.util.spec_from_loader(loader.name, loader)
    my_module = importlib.util.module_from_spec(spec)
    loader.exec_module(my_module)
    modules.append(my_module)

Note that for clarity the modules are not added to 'global namespace', i.e. sys.modules.

P.S. If you have a better solution, maybe without the need of copying files, please post it below, thanks!

John Ao
  • 89
  • 7