Extending a C++ application with python
<p>I have a legacy (but still internally maintained) application written in C++ which handles some hardware, interacts with databases, receives commands via serial line or socket... in short it does a non-trivial amount of work.</p>

<p>This application runs under Linux (ARM/Buildroot).</p>

<p>Now the need is to revamp the control interface adding a RESTful API.</p>

<p>I am exploring the possibility to do so via a Python extension.</p>

<p>Note I am a C++/java programmer and I'm not really proficient in Python, but I know the basics.</p>

<p>General idea would be:</p>

<ul>
<li>Start a Python interpreter as a thread in the C++ application.</li>
<li>Use Flask/jinja2 (or just plain Bottle) to handle incoming RESTful requests.</li>
<li>Expose a few (perhaps just one) C++ class to Python.</li>
<li>Call from Python the appropriate C++ methods to perform the required actions.</li>
</ul>

<p>I studied the official documentation (mainly pertaining plain C) and several alternatives, including:</p>

<ul>
<li>Boost.Python (possibly too heavy for our limited hardware)</li>
<li>pybind11 (seems concerned only in extending Python, not embedding it).</li>
<li><a href="http://www.codeproject.com/Articles/11805/Embedding-Python-in-C-C-Part-I" rel="nofollow">http://www.codeproject.com/Articles/11805/Embedding-Python-in-C-C-Part-I</a> (deals only with embedding, without giving Python access to C++ classes).</li>
</ul>

<p>Question are:</p>

<ul>
<li>does this make any sense?</li>
<li>what is the least intrusive way (if any) to achieve this?</li>
<li>which lib/framework is advised to use?</li>
<li>is there some tutorial project along these lines?</li>
</ul>

<p>I know the question is quite broad, but I hope to narrow it as soon as the first comments will point me in the right direction.</p>

<p>Can you do it the other way around - embed your C++ code in Python program? This way it would be more prepared for moving existing functionality Python like you said.</p>

<p>Python makes a lot of things easier (faster to develop, easier to maintain) - communication with databases, libraries (if hey have Python wrappers), process/thread management... Keep in C++ just the stuff that needs to be in C++, like dealing with hardware, C/C++-only libraries, CPU-heavy code.</p>

<p>Have a look at <a href="http://cython.org/" rel="nofollow">Cython</a> for embedding C++ in Python. Cython is basically a compiler from Python-like language to a .c/.cpp file that "just" calls the Python C API, so it can be used from "normal" Python code, but can also call other C/C++ code.</p>

<p><strong>Alternative:</strong> Implement an API in the C++ application and create a separate Python application that uses this API. By API I don't mean REST API, but something more low-level - RPC, ZeroMQ, plain sockets...</p>

python c++ extending python-embedding