Calling python from C with OpenMP

Øystein Schønning-Johansen oysteijo at gmail.com
Fri May 13 12:22:36 EDT 2016


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



More information about the Python-list mailing list