Is Python/C api thread safety enough?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Casper

    #1

    Is Python/C api thread safety enough?

    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_lon g_time()
    Py_END_ALLOW_TH READS
    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_lon g_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_lon g_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



  • Nick Coghlan

    #2
    Re: Is Python/C api thread safety enough?

    Casper wrote:[color=blue]
    > Can sombody tell me is this possible? Thanks first.[/color]

    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_Ensu re when it needs to call the Python C/API.

    Cheers,
    Nick.

    Comment

    Working...