[C++-sig] Passing messages from C++ to python

Ram, Siddharth sram at qualcomm.com
Thu Jun 23 20:46:25 CEST 2005


I  have a bunch of C++ code that has sucessfully been exported to
python. I have a question about how I could use boost to send messages
from C++ to Python. The setup is somewhat complex, please bear with me

Here is my setup:

1 or more C++ (p)threads are executing

I have started the python interpreter in another pthread

I import the .so created via boost into the interpreter (which is
attached to /dev/tty)

So now, I can call methods in the C++ threads from the python
interpreter. All commands exported to  commands result in messages being
posted on a queue. It gets picked up by the C++ thread and consumed.


Now, I would like to be able to do this from the C++ thread to Python.
Imagine that some timer expires in the C++ thread and it wants to send a
message to a listener in python. I have created another pthread which
gets signaled via a condition variable, recieves the message, and does a
PyRun_SimpleString (after grabbing the global interpreter lock and all
that good stuff). 

My question is this: How do I pass parameters from C++ to Python receive
thread ? They use the same types which I have exported to python via
Boost, but Py_BuildValue does not have a way of saying '..take this C++
object and create this corresponding PyObject which you already know
about'. The only way I can think of doing this is by writing a
conversion function, which I am trying to avoid. And I should not really
need to do this, since Python already knows the type. Can Boost help
with this?

Thanks for any help. BTW, If it is not obvious, I am a newcomer to
Boost.Python. It really rocks. Thanks to David and others for an awesome
tool.



More information about the Cplusplus-sig mailing list