On 2016-05-13 17:22, Øystein Schønning-Johansen wrote:
On Friday, May 13, 2016 at 2:04:53 AM UTC+2, Sturla Molden wrote:
You must own the GIL before you can safely use the Python C API, object
creation and refcounting in particular. Use the "Simplified GIL API" to
grab the GIL and release it when you are done.

I've now read about the GIL and it looks like I am in deep problems.

I've added the GILState lock to the threaded loop like this:

#pragma omp parallel for
    for( int i = 0; i < 10; i++ ){
        PyGILState_STATE gstate;
        gstate = PyGILState_Ensure();
        PyObject *ret = PyObject_CallMethod( mult_obj, "do_multiply", "i", i );
        if( !ret ){
            printf("Cannot call 'do_multiply'\n");
            continue;
        }
        printf("The value calculated in Python was: %3d\n", (int) 
PyLong_AsLong(ret));
        Py_DECREF(ret);
        PyGILState_Release(gstate);
    }

.... but still no success. Have I done it right?

regs,
-Øystein

Every PyGILState_Ensure call must be matched with a PyGILState_Release call. The way it's currently written, it won't call PyGILState_Release if ret is NULL.

However, I don't think you'll gain much here because you can gain from multi-threading only if the threads can run in parallel. You need to hold the GIL while making Python calls, and only 1 thread can hold the GIL at any time.

--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to