Releasing GIL in unknown state

F

Farshid Lashkari

Hi,

Is there a proper way for C code to ensure that the GIL is released? I
know about PyGILState_Ensure and PyGILState_Release, but that is to
ensure the GIL is acquired. I have some extension code that does heavy
processing and I want to release the GIL, but the code can't be sure
that the GIL is already acquired. All I can think of so far is the
following:

PyGILState_STATE state = PyGILState_Ensure();

Py_BEGIN_ALLOW_THREADS

//do processing

Py_END_ALLOW_THREADS

PyGILState_Release(state)


Is there a better way?

-Farshid
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top