0

I have a shared library written in C and called in Python.

In a pthread, some Python C API called between PyGILState_Ensure() and PyGILState_Release().

The value of PyGILState_STATE remains the same all the time.

c code

PyGILState_STATE st = PyGILState_Ensure()
printf("After Ensure, PyGILState_STATE == %d\n", st);
// Python C API call
PyGILState_Release(st);
printf("After Release, PyGILState_STATE == %d\n", st);

output

After Ensure, PyGILState_STATE == 1
After Release, PyGILState_STATE == 1

According to its definition, 1 means PyGILState_UNLOCKED.

typedef
    enum {PyGILState_LOCKED, PyGILState_UNLOCKED}
        PyGILState_STATE;

Why didn't the state change ?

Jack
  • 55
  • 2
  • 7
  • 3
    You're printing the local variable, not the real GIL – o11c Jan 14 '22 at 06:12
  • PyGILState_Ensure()/PyGILState_Release() call on st will not change it? – Jack Jan 14 '22 at 06:17
  • 1
    You need to reread the docs; the functions don't do what you think they do. https://docs.python.org/3/c-api/init.html#c.PyGILState_Ensure – o11c Jan 14 '22 at 06:59

0 Answers0