[Python-Dev] GIL required for _all_ Python calls?

John Arbash Meinel john.arbash.meinel at gmail.com
Thu Jan 7 04:25:48 CET 2010

MRAB wrote:
> Hi,
> I've been wondering whether it's possible to release the GIL in the
> regex engine during matching.
> I know that it needs to have the GIL during memory-management calls, but
> does it for calls like Py_UNICODE_TOLOWER or PyErr_SetString? Is there
> an easy way to find out? Or is it just a case of checking the source
> files for mentions of the GIL? The header file for PyList_New, for
> example, doesn't mention it!
> Thanks

Anything that Py_INCREF or Py_DECREF's should have the GIL, or you may
get concurrent updating of the value, and then the final value is wrong.
(two threads do 5+1 getting 6, rather than 7, and when the decref, you
end up at 4 rather than back at 5).

AFAIK, the only things that don't require the GIL are macro functions,
like PyString_AS_STRING or PyTuple_SET_ITEM. PyErr_SetString, for
example, will be increfing and setting the exception state, so certainly
needs the GIL to be held.


More information about the Python-Dev mailing list