Le 10/08/2020 à 19:04, Sebastian Berg a écrit :
In that case, is
PyGILState_Ensure()considered safe and appropriate API to be used within "third-party C function" (and/or NumPy API)? I used to generally think its not, but now it seems to me it should be, if the thread we are running on was previously spawned by Python?
PyGILState_Ensure()should always be safe, regardless of who/what spawned the thread. The only limit is that it doesn't handle subinterpreters well.
Yes, sorry, to be clear: I am worried about subinterpreters. Not because I feel its pressing, or because I particularly like the topic... But if I create new public API now, I don't want to have to extend it in 2 years for something I can anticipate now. At least in cases where adding the API does not hurt.
In this case, for future-proofing, you may want to take a
PyInterpreterState* argument, because that is probably going to be how
CPython approaches things (i.e. create a new API like
PyGILState_Ensure(), but that knows about different interpreters).
I would also recommend you chime in on https://bugs.python.org/issue15751 and describe your dilemma there.
(cc Eric Snow, who works on improving subinterpreters, and whom I'm not sure is on this list)