2

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.

This application runs under Linux (ARM/Buildroot).

Now the need is to revamp the control interface adding a RESTful API.

I am exploring the possibility to do so via a Python extension.

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

General idea would be:

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

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

Question are:

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

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.

ZioByte
  • 2,690
  • 1
  • 32
  • 68
  • Is there really a need to have the REST API embedded in the application? If you already have the socket based control interface, why not have the REST API use that (and be as a standalone app)? – Dan Mašek Sep 30 '16 at 21:14
  • @DanMašek: Current control interfaces are very limited and do not cover database setup, which has to be done manually. Also reporting capabilities are rather restricted. I plan to implement some of the database access facilities directly in Python and to give better responses to functional queries. Basically We are exploring possibility to further enhance current Application keeping the existing as-is (more or less) and adding new functionality mainly in Python. Further development would be to move existing functionality from C++ to Python and drop legacy interfaces (far future!). – ZioByte Sep 30 '16 at 22:26

1 Answers1

2

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.

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.

Have a look at Cython 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.

Alternative: 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...

Messa
  • 24,321
  • 6
  • 68
  • 92