Welcome, guest | Sign In | My Account | Store | Cart

To use multiple threads, you must release the Python thread-lock. The simplest way with SWIG is to use an except directive.For example, Mark Hammond does the following in the Win32 extensions:

Python, 21 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Typemap for SWIG by Mark Hammond, as posted on comp.lang.python
//
//
// Map API functions that return BOOL to
// functions that return None, but raise exceptions.
// These functions must set the win32 LastError.

// These functions automatically release the thread lock for the
// duration of the function
// 
%typedef BOOL BOOLAPI

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