Threading problems with SWIG generated code

Mark Hammond MarkH at ActiveState.com
Sun Feb 25 16:46:38 EST 2001


Yves Perrenoud wrote:

> I've ran into some problems when trying to use multiple threads which
> make use of modules generated with SWIG. Here's the shortest example I
> could come up with to illustrate the problem:

[example snipped]

You need to release the Python thread-lock.  The simplest way with SWIG is to use an except directive.

For example, I do the following in the Win32 extensions:

// Map API functions that return BOOL to
// functions that return None, but raise exceptions.
// These functions must set the win32 LastError.
%typedef BOOL BOOLAPI

%typemap(python,except) BOOLAPI {
      Py_BEGIN_ALLOW_THREADS
      $function
      Py_END_ALLOW_THREADS
      if (!$source)  {
           $cleanup
           return PyWin_SetAPIError("$name");
      }
}

This gives me 2 things to functions I declare as "BOOLAPI"
* Ensures the thread-lock is released when the function is called.
* Map failures to Win32 error codes.

Hope this helps...

Mark.




More information about the Python-list mailing list