7

I've got a library written in C++ which I wrap using SWIG and use in python. Generally there is one class with few methods. The problem is that calling these methods may be time consuming - they may hang my application (GIL is not released when calling these methods). So my question is:

What is the simplest way to release GIL for these method calls?

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

NoDataDumpNoContribution
  • 10,591
  • 9
  • 64
  • 104
uhz
  • 2,468
  • 1
  • 20
  • 20

3 Answers3

9

Not having any idea what SWIG is I'll attempt an answer anyway :)

Use something like this to release/acquire the GIL:

class GILReleaser {
    GILReleaser() : save(PyEval_SaveThread()) {}

    ~GILReleaser() {
        PyEval_RestoreThread(save);
    }

    PyThreadState* save;
};

And in the code-block of your choosing, utilize RAII to release/acquire GIL:

{
    GILReleaser releaser;
    // ... Do stuff ...
}
Henrik Gustafsson
  • 51,180
  • 9
  • 47
  • 60
9

The real problem is that SWIG is not documented well (I saw hints to use changelog for searching ;) ).

Ok, I found out that I can do inline functions in SWIG and use macros to release/acquire 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 not present in original C++, but available in python module. This is (almost) exactly what I wanted. (what I would like is to wrap original method like python decorator does)

uhz
  • 2,468
  • 1
  • 20
  • 20
  • Use Henrik's. This is right, but is bad c++ practice. Also I had a crash extracting data from a dict after releasing the GIL, so I generally don't use any functions from python.h between GIL release and reacquire. – Kenny Ostrom Jun 03 '15 at 13:10
0

You can use the same API call as for C. No difference. Include "python.h" and call the appoproate function.

Also, see if SWIG doesn't have a typemap or something to indicate that the GIL shuold not be held for a specific function.

Macke
  • 24,812
  • 7
  • 82
  • 118