Embedded python with threads

Greg Chapman glc at well.com
Thu Sep 16 11:21:54 EDT 2004


On Wed, 15 Sep 2004 18:05:54 +0200, nik <my.news.groups at noos.fr> wrote:

>////
>Py_Initialize();
>
>// PyImport_ImportModule blocks until the myModule script has run
>// through...
>PyObject* module = PyImport_ImportModule("myModule");
>
>PyObject* function = PyObject_GetAttrString(module, "GetList");
>while(1) {
>	PyObject* pyList = PyObject_CallFunction(function, "");
>	// use the stuff in pyList
>	sleep(15);
>	}

I'm not sure I fully understand your problem, but in the loop above, you should
have:

        Py_BEGIN_ALLOW_THREADS
        sleep(15);
        Py_END_ALLOW_THREADS

This releases the Python GIL during the sleep call, allowing your list-filling
thread to run.

>
># the following statement happens when the C++ app imports the module,
># but the thread only lives as long the main python thread (which is
># over straight away on my linux PC)
>

This is what I don't understand, since the Python main thread should be the
thread on which Py_Initialize was called; it should remain alive (from Python's
perspective) until Py_Finalize is called.

---
Greg Chapman




More information about the Python-list mailing list