スレッド間の相互作用のパターンを変更することをお勧めします。
現在の実装は次のように機能しているようです。
PyGILState_Ensure();
result = doDangerousThing(); // this could kill the thread
storeToPythonObject(result);
PyGILState_Release();
バリアント A、明白:
result = doDangerousThing(); // this could kill the thread
PyGILState_Ensure();
storeToPythonObject(result);
PyGILState_Release();
バリアント B、それほど明白ではありません。アイデアについては[1]、[2]を参照してください。
/* worker_thread.c */
result = doDangerousThing(); // this could kill the thread
putToLocklessQueue(result, *queue_in_main_thread);
/* main_thread.c */
if (hasItems(my_lockless_queue)) {
PyGILState_Ensure();
while (hasItems(my_lockless_queue)) {
storeToPythonObject(popItem(my_lockless_queue));
}
PyGILState_Release();
// sleep again here
}