Using multithreading: SWIG C++ Python polymorphism and multi-threading on newest questions tagged multithreading – Stack Overflow
I’m integrating a 3rd party C++ package to a python application using SWIG. The package connects to a proprietary API over a network and receives updates. The overall flow is that python instantiates a C++ object, calls its functions to set it up and then waits for the updates.
I implemented a callback mechanism for the updates using SWIG’s directors feature, and while testing from python, or from C++ functions called by python, it works well. Namely I’m able to inherit a C++ class in Python, call its virtual functions from C++ and see the python code take priority and execute.
When I receive the updates from the network I get:
The thread 'Win32 Thread' (0x1f78) has exited with code 0 (0x0). Unhandled exception at 0x1e0650cb in python.exe: 0xC0000005: Access violation writing location 0x0000000c.
This exception is thrown from within
python27.dll while calling the callback function.
My suspicion is this: I’ve violated the GIL
AFAIU the updates come from a different thread and call python’s code using that thread.
At this point I’m at a loss. Is SWIG’s director feature limited only to flows initiated within python (i.e. from python managed threads)?
How do I circumvent this? How do I induce updates from C++ to python? Is it even possible using SWIG?
Should I use a completely different approach?
I’m open for any suggestions on this matter…
Using multithreading: using-multithreading