PyEval_InitThreads in Python 3: How/when to call it? (the saga continues ad nauseam)

前端 未结 7 1077
别那么骄傲
别那么骄傲 2020-11-27 03:06

Basically there seems to be massive confusion/ambiguity over when exactly PyEval_InitThreads() is supposed to be called, and what accompanying API

7条回答
  •  清歌不尽
    2020-11-27 03:54

    The suggestion to call PyEval_SaveThread works

    PyEval_InitThreads();
    PyThreadState* st = PyEval_SaveThread();
    

    However to prevent crash when module is imported, ensure Python APIs to import are protected using

    PyGILState_Ensure and PyGILState_Release

    e.g.

    PyGILState_STATE gstate = PyGILState_Ensure();
    PyObject *pyModule_p = PyImport_Import(pyModuleName_p);
    PyGILState_Release(gstate);
    

提交回复
热议问题