Embedded Threading Question
Tim Dietz
tpdietz at hotmail.com
Mon Dec 2 18:58:08 EST 2002
Thorsten Goertz <thorsten at goertz.com> wrote in message news:<3DCADE0D.2E06C5E3 at goertz.com>...
> Tim Dietz wrote:
>
> > When I test the module within the Python interpreter from
> > DOS, it's happy as a clam. But, the problem is that when I
> > include the exact same module within the application, the
> > app hangs forever and I have to kill the process.
>
Sorry it's taken me so long to reply. I was put on a different
part of the project. Now I'm back, but I still do not know how
to proceed. Here is my situation again and some sample code.
I have a C++ GUI application that essentially provides a memo to
emulate the Python command line (>>>). STDOUT is redirected to a
memo in a different window. Upon initialization, there is no
support for Python threads--none was needed until now. Now, we
are importing a file that creates and starts a thread that will
continue executing during the life of the application. And,
actually, the thread works very well, until I try to call a C
function from Python. That's when the application hangs. If
I do not try to interact with C from this new Python thread,
the application is happy. I can call these same C functions
from the main thread in Python, but not from a thread other than
main. I am using python22.dll.
>From what I've read (in 'Embedding Python in Multi-Threaded C/C++
Applications'), I kind of understand about thread initialization,
and acquiring and releasing the lock while executing python statements.
My question is, do I need to wrap every single grouping of Py*()
function calls within an acquire/release lock pair, or are there
special calls that I should target. I'm asking this, because the
Py*() calls are dispersed throughout a few files and I'm not sure
how to go about coding these so that I can have threads in my
Python code work.
A sample of the main init code follows (Borland C++Builder 3):
Py_Initialize();
PySys_SetArgv(_argc, _argv); // let Python see Borlandish args to Cntlr
// Add our own script directory to the module search path
if (PyRun_SimpleString("import sys"))
return;
cmd = String("sys.path.insert(0, '") +
AUTOLOAD_SCRIPT_DIR +
"')" ;
if (PyRun_SimpleString(cmd.c_str()))
return;
// Autoload our primitives that are defined in Python.
// Exec, not import, these so that they
// are available in main namespace.
cmd = String("execfile('") +
AUTOLOAD_SCRIPT_DIR + "\\\\" +
AUTOLOAD_PRIMS_FILE + "')";
if (PyRun_SimpleString(cmd.c_str()))
return;
... then, in the destructor for this class, we only have
the following statement...
Py_Finalize();
More information about the Python-list
mailing list