[Python-Dev] embedding in multi threaded C/C++

George Fazekas gyorgy.fazekas at elec.qmul.ac.uk
Mon Mar 10 13:11:24 CET 2008


Hi all,

I'm working on embedding Python in a multi threaded application
but found mostly old or confusing info on this. Can anyone point me to  
the
right direction or send some working examples?

Detail:

Python 2.5.1, MacOSX Leopard 10.5.1, using Pytohn/C API

The application initializes Python in a shared library, which in turn  
links
in more libraries that may or may not use C API commands in parallel.
Generally it all works fine, but when two libraries try to access  
Python code
I get seg fault or similar.

The closest I got to resolve this is based on this message:
http://groups.google.fi/group/comp.lang.python/msg/fe4e114d1e1a741d
which suggests starting a new sub interpreter for each task.

However, i still get errors like below. (Thread 0 on it's own works  
fine.)
According to the docs PyObject_HasAttrString should always succeeds so  
I don't
understand what happens.

Also I get thread mix-up messages randomly even though I double  
checked the
implementation.

2 Threads accessing Python:
-------------------------------

Thread 0 Crashed:
0   org.python.python             	0x15a58bcc PyErr_Occurred + 16  
(errors.c:77)
1   org.python.python             	0x159c642c instance_getattr + 277  
(classobject.c:698)
2   org.python.python             	0x159f789b PyObject_HasAttrString +  
116 (object.c:1069)

While Thread 4 is running a process:
----------------------------------------
Thread 4:
0   org.python.python             	0x15a43751 PyEval_EvalFrameEx + 794  
(ceval.c:852)
1   org.python.python             	0x15a49cdc PyEval_EvalCodeEx + 1819  
(ceval.c:2831)
2   org.python.python             	0x159df537 function_call + 320  
(funcobject.c:517)
3   org.python.python             	0x159be278 PyObject_Call + 45  
(abstract.c:1860)
4   org.python.python             	0x159c5ee5 instancemethod_call +  
401 (classobject.c:2497)
5   org.python.python             	0x159c297c  
PyObject_CallMethodObjArgs + 223 (abstract.c:1860)


Thanks for any advice,
George



More information about the Python-Dev mailing list