Is Python/C api thread safety enough?

C

Casper

i make a multi thread C/C++ program emded python, each thread running a python sub-interpreter, to make it safety i make code as following between acquire/release the globle interpreter lock:
Py_BEGIN_ALLOW_THREADS
func_take_a_long_time()
Py_END_ALLOW_THREADS
according to the python document this will avoid to block other thread that running python code since the function will take a long time, but i am wandered if the code of func_take_a_long_time() call a Python/C api, is this conflict with other threads and cause fatal error? i have release the interpreter lock in Py_BEGIN_ALLOW_THREADS and without protected from thread conflict, so if the func_take_a_long_time() call a Python/C api, it perhaps use other thread's context to running python code.

Can sombody tell me is this possible? Thanks first.


Donnie Leen
 
N

Nick Coghlan

Casper said:
Can sombody tell me is this possible? Thanks first.

If you're going to call the Python C/API, you need to be holding the GIL. So you
either can't release it, or the long-running function has to use
PyGILState_Ensure when it needs to call the Python C/API.

Cheers,
Nick.
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top