I have the following code, that seems to make sense to me.
However, it crashes about 1/3 of the times.
My platform is Python 2.4.1 on WXP (I tried the release version from
the msi and the debug version built by me, both downloaded today to
have the latest version).
The crash happens while the main thread is in Py_Finalize.
I traced the crash to _Py_ForgetRefer ence(op) in object.c at line 1847,
where I have op->_ob_prev == NULL.
What am I doing wrong? I'm definitely not too sure about the way I'm
handling the GIL.
Thanks in adv for any suggestion
Cheers and ciao
Ugo
////////////////////////// TestPyThreads.p y //////////////////////////
#include <windows.h>
#include "Python.h"
int main()
{
PyEval_InitThre ads();
Py_Initialize() ;
PyGILState_STAT E main_restore_st ate = PyGILState_UNLO CKED;
PyGILState_Rele ase(main_restor e_state);
// start the thread
{
PyGILState_STAT E state = PyGILState_Ensu re();
int trash = PyRun_SimpleStr ing(
"import thread\n"
"import time\n"
"def foo():\n"
" f = open('pippo.out ', 'w', 0)\n"
" i = 0;\n"
" while 1:\n"
" f.write('%d\\n' %i)\n"
" time.sleep(0.01 )\n"
" i += 1\n"
"t = thread.start_ne w_thread(foo, ())\n"
);
PyGILState_Rele ase(state);
}
// wait 300 ms
Sleep(300);
PyGILState_Ensu re();
Py_Finalize();
return 0;
}
However, it crashes about 1/3 of the times.
My platform is Python 2.4.1 on WXP (I tried the release version from
the msi and the debug version built by me, both downloaded today to
have the latest version).
The crash happens while the main thread is in Py_Finalize.
I traced the crash to _Py_ForgetRefer ence(op) in object.c at line 1847,
where I have op->_ob_prev == NULL.
What am I doing wrong? I'm definitely not too sure about the way I'm
handling the GIL.
Thanks in adv for any suggestion
Cheers and ciao
Ugo
////////////////////////// TestPyThreads.p y //////////////////////////
#include <windows.h>
#include "Python.h"
int main()
{
PyEval_InitThre ads();
Py_Initialize() ;
PyGILState_STAT E main_restore_st ate = PyGILState_UNLO CKED;
PyGILState_Rele ase(main_restor e_state);
// start the thread
{
PyGILState_STAT E state = PyGILState_Ensu re();
int trash = PyRun_SimpleStr ing(
"import thread\n"
"import time\n"
"def foo():\n"
" f = open('pippo.out ', 'w', 0)\n"
" i = 0;\n"
" while 1:\n"
" f.write('%d\\n' %i)\n"
" time.sleep(0.01 )\n"
" i += 1\n"
"t = thread.start_ne w_thread(foo, ())\n"
);
PyGILState_Rele ase(state);
}
// wait 300 ms
Sleep(300);
PyGILState_Ensu re();
Py_Finalize();
return 0;
}