[capi-sig] What can I do without GIL?
Stefan Behnel
python_capi at behnel.de
Mon Oct 18 12:35:44 CEST 2010
Nikolaus Rath, 18.10.2010 04:02:
> I a thread does not hold the GIL, is it totally forbidden to call any
> Py* API functions at all, or are there some exceptions?
>
> More concretely, am I allowed to create fresh Python objects with
> PyBytes_FromString et. al. without holding the GIL?
No. Anything that requires Python memory management and especially
reference counting must be protected by the GIL. You can read the C pointer
and length of a Python string without the GIL (using the C-API macros) and
you can acquire and release thread locks, but you can't create new objects.
Even simple looking functions may raise exceptions in some corner cases
(such as memory errors), which also requires the GIL to work. So don't
expect there to be much that you can do without holding it. The best way to
tell is to look through the CPython implementation of the function you want
to call, including all other functions that it calls, and to check for
anything that looks like reference counting or exception handling.
Stefan
More information about the capi-sig
mailing list