Embedded python with threads

nik my.news.groups at noos.fr
Thu Sep 16 19:05:13 EDT 2004


Greg Chapman wrote:
> 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.
> 

I think I was making an assumption that since the list filling threads 
didn't run that the import module was calling the create thread command, 
then exiting (killing the children threads along with it) - I'm a novice 
at threads with python, so I'm probably misunderstanding the general 
picture. However, maybe your suggestion of the Py_BEGIN_ALLOW_THREADS 
will make the difference. I'll try again as soon as I can (not until 
next week now)...

many thanks for the reply,
nik

> ---
> Greg Chapman
> 



More information about the Python-list mailing list