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

Petrucio petrucio at hoplon.com
Thu Jun 23 21:00:41 CEST 2005


I did not took the time to fully read your question and setup, so forgive 
me if my answer is not what you need.

But to pass parameters or call code from C++ to Python, the easiest way to 
do this is to implement a polymorphic function in C++ to receive these, and 
reimplement that in Python. Something like this:

class CppReceiver {
    public:
       virtual void receiveParameters(SomeClass parm1, ...) {}
}

Export this class and reimplement that in Python, doing whatever you want 
in it:

def class PythonReceiver:
    def receiveParameters(parm1, ...):
         ...

Now all you have to do is create an instance of that extended python class 
and pass it to the c++ code that will do the calling. When any C++ code 
calls the receiveParameters of your python create instance, the python code 
you implemented will be called.

Petrucio

At 15:46 23/6/2005, you wrote:
>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.
>_______________________________________________
>C++-sig mailing list
>C++-sig at python.org
>http://mail.python.org/mailman/listinfo/c++-sig




More information about the Cplusplus-sig mailing list