[Python-Dev] GIL required for all Python calls? (original) (raw)

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 PyINCREF or PyDECREF'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).

Correct.

AFAIK, the only things that don't require the GIL are macro functions, like PyStringASSTRING or PyTupleSETITEM. PyErrSetString, 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.

-- Regards, Benjamin



More information about the Python-Dev mailing list