Shared Python Swig Modules

1 day ago 1
ARTICLE AD BOX

I have a complex C++ application (call it foo) that exposes an API that is wrapped for Python via Swig.

This provides a 'statefull' library -- i.e. large amounts of complex data are kept on the C++ side of things, the API mostly consists of relatively simple function calls that manipulate / interrogate the state of the model.

I recently developed another C++ application (bar) that uses the first (foo) as a library (directly through C++ #include and static linking).

I have now also wrapped bar with Swig. It has it's own *.i file.

This results in:

foo.py _foo.so bar.py _bar.so

Using either foo or bar from Python seems to work as expected.

However, if I load both foo and bar into Python at the same time, they seem to interact and behave in strange ways. It seems like they are sharing the same state on the C++ side of things. This is unexpected (to me). I would expect them to have entirely independent memory regions, copies of code, etc.

Is my expectation wrong? If so, is there a Swig or Python option to change this behavior? Is it possible to wrap the C++ side of things in an extra namespace from the *.i file?

Read Entire Article