我有一个非常精细的例子,它创建了一个我似乎无法摆脱的段错误。 Python脚本在扩展中调用C函数,该函数使用pthread创建新线程。我在新线程的python调用(PyRun_SimpleString)周围使用了PyGILState_Ensure和PyGILState_Release,但也许我没有正确使用它们或错过了其他一些步骤。在receive_audio函数中注释掉python调用,segfault不再发生。有任何想法吗?Segfault在C多线程python扩展中
输出:
蟒蛇的lib/test.py
(主线程)initmodule完整
(主线程)调用run_thread()
(主线程)创建线程
(新主题)在receive_audio () - 获取GIL
(新主题)python print!
(新主题)在receive_audio() - 释放GIL
(新主题)循环0
分段故障
C代码如下:
PyMODINIT_FUNC streamaudio() {
PyObject *m = Py_InitModule("streamaudio", methods);
PyEval_InitThreads();
mainThreadState = PyThreadState_Get();
PyEval_ReleaseLock();
printf("(Main Thread) initmodule complete\n");
}
static PyObject* run_thread(PyObject* self, PyObject* args)
{
int ok, stream_id;
PyGILState_STATE gstate;
gstate = PyGILState_Ensure();
ok = PyArg_ParseTuple(args, "i", &stream_id);
PyRun_SimpleString("print '(Main Thread) Creating thread'\n");
int rc = pthread_create(&thread, NULL, receive_audio, (void*)stream_id);
PyRun_SimpleString("print '(Main Thread) Thread created'\n");
PyGILState_Release(gstate);
return Py_BuildValue("i", rc);
}
void* receive_audio(void *x)
{
printf("(New Thread) In receive_audio() - acquiring GIL\n");
PyGILState_STATE gstate;
gstate = PyGILState_Ensure();
PyRun_SimpleString("print '(New Thread) python print!'\n");
PyGILState_Release(gstate);
printf("(New Thread) In receive_audio() - released GIL\n");
int i;
for (i = 0; i < 100; i++) {
printf("(New Thread) Looping %d\n", i);
sleep(1);
}
}
请注意 - 围绕pthread_create函数调用PyGILState_Ensure()和PyGILState_Release似乎是无关紧要的。我认为这是可以预料的,因为它是由主线程运行的。 – cursemyziti 2010-03-19 16:37:27