title: SWIG C++ Python polymorphism and multi-threading
body: <p>I'm integrating a 3rd party C++ package to a python application using <a href="http://www.swig.org/" rel="noreferrer">SWIG</a>. 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.</p>
  
  <p>I implemented a callback mechanism for the updates using <a href="http://www.swig.org/Doc2.0/Python.html#Python_directors" rel="noreferrer">SWIG's directors feature</a>, 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.</p>
  
  <p><strong>The problem:</strong><br>
  When I receive the updates from the network I get:</p>
  
  <pre><code>The thread 'Win32 Thread' (0x1f78) has exited with code 0 (0x0).
  Unhandled exception at 0x1e0650cb in python.exe: 0xC0000005: Access violation writing location 0x0000000c.
  </code></pre>
  
  <p>This exception is thrown from within <code>python27.dll</code> while calling the callback function.<br>
  <strong>My suspicion is this:</strong> <em>I've violated the <a href="http://wiki.python.org/moin/GlobalInterpreterLock" rel="noreferrer">GIL</a></em><br>
  AFAIU the updates come from a different thread and call python's code using that thread.</p>
  
  <p>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)?<br>
  How do I circumvent this? How do I induce updates from C++ to python? Is it even possible using SWIG?<br>
  Should I use a completely different approach?</p>
  
  <p>I'm open for any suggestions on this matter...</p>