segfault when calling Python from C thread

Fredrik Lundh fredrik at pythonware.com
Sun Feb 20 10:12:13 EST 2005


Greg Chapman wrote:

> Your callback function needs to hold the Python GIL (and have a vaild
> threadstate) before it calls any Python C-API functions.  Change the
> last part of it to:
>
>    PyGILState_STATE state;
>
>    /* ... */
>
>    /* Time to call the callback */
>
>    state = PyGILState_Ensure();
>
>    arglist = Py_BuildValue("(s)", str);
>    result = PyEval_CallObject(my_callback, arglist);
>    Py_DECREF(arglist);
>    if(result == NULL)
>        return;
>    Py_DECREF(result);
>
>    PyGILState_Release(state);
> }

you might wish to make sure you release the GIL even if the callback
raises an exception...

</F> 






More information about the Python-list mailing list