Ask Your Question
0

How does the GIL release happen for drawing functions exposed to Python?

asked 2018-01-09 10:49:45 -0600

Agost Biro gravatar image

I'm trying to determine whether the Python drawing functions release the Global Interpreter Lock (GIL). I have gotten as far finding the ERRWRAP2 macro, but I’m not sure if it is applied to the drawing functions defined in drawing.cpp, as according to the docs these should be prefixed with the CV_WRAP macro, but they are not. Could somebody point me to where/how the GIL release happens for the drawing functions exposed to Python please?

edit retag flag offensive close merge delete

Comments

1

why do you think, it even does release the GIL, ever ?

(it never happens, to my knowledge)

(if so, it would look like this , no ? )

berak gravatar imageberak ( 2018-01-09 11:55:33 -0600 )edit
1

The ERRWRAP2 macro prepends a PyAllowThreads guard object to every function call that releases the GIL with a call to PyEval_SaveThread when constructed and acquires it when it is destructed. The Py_BEGIN_ALLOW_THREADS macro that you linked to expands to a PyEval_SaveThread call (and more) so they are equivalent.

The preferred way for GIL release in C++ is to use a guard class like PyAllowThreads for better exception handling, while C programs tend to use the Py_BEGIN_ALLOW_THREADS macro.

Agost Biro gravatar imageAgost Biro ( 2018-01-10 08:01:34 -0600 )edit

oh, thanks, i didn't knew that !

berak gravatar imageberak ( 2018-01-10 08:02:57 -0600 )edit
1

btw, the CV_WRAP macro is for class member functions, "free" functions need a CV_EXPORTS_W instead, and it has to happen in the interface header, not in the cpp implementation file

berak gravatar imageberak ( 2018-01-10 08:16:13 -0600 )edit

1 answer

Sort by » oldest newest most voted
1

answered 2018-01-10 08:11:38 -0600

berak gravatar image

updated 2018-01-10 08:18:40 -0600

not sure, if it answers your question, but here's the generated code for cv2.circle():

(if you built your cv2 locally, you will find it opencv/build/modules/python_bindings_generator/pyopencv_generated_funcs.h)

actually, every c++ opencv call is wrapped in this ERRWRAP2 macro, so the answer is probably: yes !

static PyObject* pyopencv_cv_circle(PyObject* , PyObject* args, PyObject* kw)
{
    using namespace cv;

    {
    PyObject* pyobj_img = NULL;
    Mat img;
    PyObject* pyobj_center = NULL;
    Point center;
    int radius=0;
    PyObject* pyobj_color = NULL;
    Scalar color;
    int thickness=1;
    int lineType=LINE_8;
    int shift=0;

    const char* keywords[] = { "img", "center", "radius", "color", "thickness", "lineType", "shift", NULL };
    if( PyArg_ParseTupleAndKeywords(args, kw, "OOiO|iii:circle", (char**)keywords, &pyobj_img, &pyobj_center, &radius, &pyobj_color, &thickness, &lineType, &shift) &&
        pyopencv_to(pyobj_img, img, ArgInfo("img", 1)) &&
        pyopencv_to(pyobj_center, center, ArgInfo("center", 0)) &&
        pyopencv_to(pyobj_color, color, ArgInfo("color", 0)) )
    {
        ERRWRAP2(cv::circle(img, center, radius, color, thickness, lineType, shift));
        return pyopencv_from(img);
    }
    }
    PyErr_Clear();

    {
    PyObject* pyobj_img = NULL;
    UMat img;
    PyObject* pyobj_center = NULL;
    Point center;
    int radius=0;
    PyObject* pyobj_color = NULL;
    Scalar color;
    int thickness=1;
    int lineType=LINE_8;
    int shift=0;

    const char* keywords[] = { "img", "center", "radius", "color", "thickness", "lineType", "shift", NULL };
    if( PyArg_ParseTupleAndKeywords(args, kw, "OOiO|iii:circle", (char**)keywords, &pyobj_img, &pyobj_center, &radius, &pyobj_color, &thickness, &lineType, &shift) &&
        pyopencv_to(pyobj_img, img, ArgInfo("img", 1)) &&
        pyopencv_to(pyobj_center, center, ArgInfo("center", 0)) &&
        pyopencv_to(pyobj_color, color, ArgInfo("color", 0)) )
    {
        ERRWRAP2(cv::circle(img, center, radius, color, thickness, lineType, shift));
        return pyopencv_from(img);
    }
    }

    return NULL;
}
edit flag offensive delete link more

Comments

1

Perfect, thanks! This is exactly the info that I was looking for.

Agost Biro gravatar imageAgost Biro ( 2018-01-10 08:15:18 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-01-09 10:49:45 -0600

Seen: 1,034 times

Last updated: Jan 10 '18