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

Benjamin Peterson benjamin at python.org
Thu Jan 7 04:32:17 CET 2010

2010/1/6 John Arbash Meinel <john.arbash.meinel at gmail.com>:
> 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.

As a general rule, I would say, no Py* macros are safe without the gil
either (the exception being Py_END_ALLOW_THREADS), since they mutate
Python objects which must be protected.


More information about the Python-Dev mailing list