Python PyGILState_{Ensure/Release} causes segfault while returning to C++ from Python code

后端 未结 2 1512
别跟我提以往
别跟我提以往 2020-12-16 17:53

UPDATE Well, it looks like adding PyEval_InitThreads() before the call to PyGILState_Ensure() does the trick. In my haste to figure things out I incorrectly

2条回答
  •  执念已碎
    2020-12-16 18:37

    I ran into this exact problem as well. The documentation for anything relating to threads in CPython is unfortunately patchy at best.

    Essentially, you need to do the following:

    In your main thread, BEFORE any other threads are spawned, you need to call PyEval_InitThreads(). A good place to do this is right after you call PyInitialize().

    Now, PyEval_InitThreads() not only initializes the Python interpreter thread-state, it also implicitly acquires the Global Interpreter Lock. This means, you need to release the lock before you call PyGILEnsure_State() in some other thread, otherwise your program will hang. You can do this with the function PyEval_ReleaseLock().

    So basically, in your main thread, before any other threads are launched, you want to say:

    PyInitialize();
    PyEval_InitThreads();
    PyEval_ReleaseLock();
    

    Then, in any additional thread, anytime you use the Python API you need to say:

    PyGILState_STATE gstate;
    gstate = PyGILState_Ensure();
    
    /* ... some code that does things with Python ... */
    
    PyGILState_Release(gstate); 
    

提交回复
热议问题