[Tutor] problem with threads and C app

nik my.mailing.lists at noos.fr
Wed Sep 1 17:23:32 CEST 2004


hi,

I'm trying to call some python methods from C, where the python module 
is running some threads. The python code seems to be working when I call 
the methods from the python command line, but when called from my C app, 
the thread doesn't seem to be created.

The code is below, and if it worked, I'd expect the text 'tested' to 
appear if the thread was created (plus changes to the global variable 
i). However, nothing appears...

the python code is;

import thread

i = 0
myLock = thread.allocate_lock()

def test():
    global i, orderListLock, clientLock
    print "tested"
    myLock.acquire()
    i = 50
    myLock.release()

def initialiseModule():
    global i, clientLock
#    thread.start_new_thread(test,())  <- original plan was to create 
thread here
    print "started"
    for a in range(200): pass
    myLock.acquire()
    print i       # is = 50 if I run from command line, but = 0 by 
running C app
    myLock.release()

thread.start_new_thread(test,())

and the C code is;

int main(int argc, char ** argv)
{
    PyObject * module;
    PyObject * function;
    PyObject * result;

    Py_Initialize();
    char *path, *newpath;
   
    // allows locating module in current folder
    path=Py_GetPath();
    newpath=strcat(path, ":.");
    PySys_SetPath(newpath);
    free(newpath);
   
    module = PyImport_ImportModule("testThread");
    function = PyObject_GetAttrString(module, "initialiseModule");
    result = PyObject_CallFunction(function, NULL, NULL);
    Py_Exit(0);
}


Can anyone see what I'm doing wrong?

thanks,
nik





More information about the Tutor mailing list