I was looking for some days in SWIG documentation how I could release the GIL (Global Interpreter Lock) with SWIG. There were some macros defined in the generated code, but none was used in any place.
In fact, I just had to enable the thread support with an additional argument (-threads) and now every wrapped function releases the GIL before it is called, but that does not satisfy me. Indeed, some of my wrappers must retain the GIL while they are used (see this item). So here are the features that can be used :
- nothread enables or disables the whole thread lock for a function :
- %nothread activates the nothread feature
- %thread disables the feature
- %clearnothread clears the feature
- nothreadblock enables or disables the block thread lock for a function :
- %nothreadblock activates the nothreadblock feature
- %threadblock disables the feature
- %clearnothreadblock clears the feature
- nothreadallow enables or disables the allow thread lock for a function :
- %nothreadallow activates the nothreadallow feature
- %threadallow disables the feature
- %clearnothreadallow clears the feature
When the whole thread lock is enabled, the GIL is locked when entering the C function (with the macro SWIG_PYTHON_THREAD_BEGIN_BLOCK). Then it is released before the call to the function (with SWIG_PYTHON_THREAD_BEGIN_ALLOW), retained after the end (SWIG_PYTHON_THREAD_END_ALLOW) and finally it is released when exiting the function (SWIG_PYTHON_THREAD_END_BLOCK), after all Python result variables are created and/or modified.
Thanks, i always come back to your article since i never remember those π
Do you know if there is any way to apply the feature to an entire class?
Thanks for your comment π
You should be able to specify a whole class and only this class by something like :
%thread MyClass;
I didn’t test it (not time at the moment), but it should behave like any other feature.
I don’t understand.. If I want SWIG to release the GIL during the call to function fun in class cls, should I use %thread cls::fun? Neither of the options seem to do any changes to the generated wrapper code. Do I need to do anything else?
Thankful for help..
Did you use the -threads argument?
Oops, I’m sorry. With -threads it works perfectly. (I solved it using %exception, but this is a nicer solution.)
Thanks a lot for the article!
Hi even I want want SWIG to release the GIL during the call to function only one fun in class cls, this is my interface file.
%module(“threads”=1) uiSCMM2
%{
#include “uiSCMM2.h”/// header file which has the class defined in it.
%}
%include “uiSCMM2.h”
But with this interface file, swig releases GIL for each function called of the class cls, I want it release GIL for only one function. Please suggest.
Hi,
As I’ve said, you need to add the decoration to the function that needs to release the GIL.