[capi-sig] What can I do without GIL?

Hrvoje Niksic hniksic at xemacs.org
Mon Oct 18 09:51:24 CEST 2010


Nikolaus Rath <Nikolaus at rath.org> writes:

> 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?

You are not allowed to create fresh Python objects without holding the
GIL.  The objects might implement a caching strategy which would surely
rely on the GIL for locking.

The only Python API calls that could conceivably be safe without holding
the GIL are accessors to C members of immutable objects whose reference
you're owning - say, PyString_AS_STRING, or PyInt_AS_LONG.


More information about the capi-sig mailing list