Using Python threads in an embedded application
Jody
josephwinston at mac.com
Tue Jan 15 10:26:01 EST 2002
I'm have problems with SWIG 1.3.10, python 2.2 and Python threads.
The symptom is that the first time a program is loaded the static
method _wrap_Test_getInstance works and the function exits without
error. However the second time _wrap_Test_getInstance is invoked
SWIG_NewPointerObj is returns 0.
Here's the runtime error message:
Traceback (most recent call last):
File "<string>", line 2, in ?
File "./startRecording.py", line 2, in ?
test = Test.Test_getInstance()
File "./Test.py", line 33, in __init__
self.this = this
File "./Test.py", line 8, in __setattr__
if isinstance(value,Test):
TypeError: 'NoneType' object is not callable
Exception exceptions.TypeError: "'NoneType' object is not callable" in ignored
Before I turn a bug report into SWIG, am I using Python threads
correctly as shown by the following code fragement?
#include "Python.h"
#include "Test.hpp"
static PyThreadState *globalThreadState = 0;
extern "C"
{
void initTestc(void);
}
void
initializePython(int argc, char **argv)
{
// Begin critical section
if (globalThreadState == 0)
{
/* Add static modules */
PyImport_AppendInittab("Testc", initTestc);
/* Initialize the Python interpreter. Required. */
Py_Initialize();
PySys_SetArgv(argc, argv);
/* Create and acquire the lock */
PyEval_InitThreads();
/* Release the thread state */
globalThreadState = PyEval_SaveThread();
}
// end critical section
}
void
finalizePython()
{
if (globalThreadState)
{
PyEval_AcquireThread(globalThreadState);
globalThreadState = 0;
Py_Finalize();
}
}
static char startRecordingCommand[] = "\
try: \n\
import startRecording \n\
except ImportError: \n\
print 'startRecording.py not found' \n\
";
void
startRecording(int argc, char **argv)
{
PyEval_AcquireLock();
PyThreadState *threadState = Py_NewInterpreter();
if (threadState == 0)
{
fprintf(stderr, "Sorry -- can't create an interpreter\n");
return;
}
PySys_SetArgv(argc, argv);
PyRun_SimpleString(startRecordingCommand);
Py_EndInterpreter(threadState);
PyEval_ReleaseLock();
}
int
main(int argc, char **argv)
{
initializePython(argc, argv);
startRecording(argc, argv);
startRecording(argc, argv);
finalizePython();
}
More information about the Python-list
mailing list