1

I want to integrate a Python application into my C++ application. The communication has to be bidirectional, as shown in the following example:

C++:

void main() {
    # call python code

    return 0;
}

int computeResult(int value) {
    int result;
    // do stuff with value
    return result;
}

Python:

def run(value):
    result = computeResult(value)    # call computeResult of C++ code
    ...

for i in xrange(0, 10):
    run(i)

I thought about calling the Python code from C++ via embedded Python, but I don't know how I can call computeResult from my Python code. I don't want to create a new process everytime I call computeResult, so I would have to access the existing instance of my C++ application.

Any ideas would be appreciated, even if they don't use embedded Python.

  • 1
    You can write an extension over your C function to make it available in python. – Kanishka Khandelwal Aug 11 '16 at 14:52
  • Thanks for your answer! Would that create a new process every time I call the computeResult function from my python code? – Richard Schulze Aug 11 '16 at 15:15
  • https://docs.python.org/2/extending/embedding.html this might be useful – mkarts Aug 11 '16 at 15:15
  • No, that doesn't create a new process. Basically you would need to write a python extension equivalent of your C method in C using PyObject* kind of things. Then, you can make a shared library of the same and import that module in python. Then, its just like using any other module in python. The link given by Michael would be useful. – Kanishka Khandelwal Aug 11 '16 at 15:17
  • I took a look at the link Michael posted. The paragraph [link](https://docs.python.org/2/extending/embedding.html#extending-embedded-python) seems to do exactly was I was trying to do. Thanks again to both of you! – Richard Schulze Aug 12 '16 at 12:45

0 Answers0