(Win32 API) callback to Python, threading hiccups

Tim Roberts timr at probo.com
Fri Jul 8 06:40:22 CEST 2005


Scott David Daniels <Scott.Daniels at Acm.Org> wrote:

>Francois De Serres wrote:
>>            PyGILState_STATE gil = PyGILState_Ensure();
>>            result = PyEval_CallObject(my_callback, arglist);   
>>            PyGILState_Release(gil);
>>            Py_DECREF(arglist);
>>            Py_DECREF(result);
>
>I think this should be:
>          PyGILState_STATE gil = PyGILState_Ensure();
>          result = PyEval_CallObject(my_callback, arglist);
>          Py_DECREF(arglist);
>          Py_DECREF(result);
>          PyGILState_Release(gil);
>
>The DECREFs need to be protected, that is where storage is
>recycled and such, and you still need Python's data structures
>to do that kind of work.

I freely admit to being woefully underinformed about the GIL, but I'm
wondering if your statement is really true.  If the purpose of the GIL is
simply to make things thread-safe, then I would have guessed that the first
one was correct.  If someone else holds a reference to "arglist", then the
DECREF is just a nice, atomic decrement.  If no one else holds a reference
to "arglist", then it's quite safe to delete it.

Is there more to the GIL than I'm assuming?
-- 
- Tim Roberts, timr at probo.com
  Providenza & Boekelheide, Inc.



More information about the Python-list mailing list