Releasing Python GIL in C ++ Code

I have a library written in C ++ that I wrap with SWIG and use in python. Typically, there is one class with several methods. The problem is that calling these methods can take a lot of time - they can hang my application (GIL is not issued when these methods are called). So my question is:

What is the easiest way to issue a GIL for these method calls?

(I understand that if I were using the C library, I could wrap this with some additional C code, but here I use C ++ and classes)

+6
c ++ python swig gil
source share
3 answers

Having no idea what SWIG is, I’ll try to answer anyway :)

Use something like this to free / get the GIL:

class GILReleaser { GILReleaser() : save(PyEval_SaveThread()) {} ~GILReleaser() { PyEval_RestoreThread(save); } PyThreadState* save; }; 

And in the selected code block, use RAII to issue / receive GIL:

 { GILReleaser releaser; // ... Do stuff ... } 
+5
source share

The real problem is that SWIG is not well documented (I saw tips to use the change log to search;)).

Well, I found out that I can perform built-in functions in SWIG and use macros to issue / receive GIL, it looks like this:

 %inline %{ void wrappedFunction(OriginalObject *o, <parameters>) { Py_BEGIN_ALLOW_THREADS o->originalFunction(<parameters>); Py_END_ALLOW_THREADS } %} 

This function is missing in the original C ++, but is available in the python module. This is (almost) exactly what I wanted. (I would like to wrap the original method, for example, a python decorator)

+5
source share

You can use the same API call as for C. There is no difference. Turn on "python.h" and call the appoproate function.

Also, see if there is a SWIG card or something to indicate that the GIL shuold is not held for a specific function.

+1
source share

All Articles