Python for embedded systems developers
What is Python good for in Embedded Systems?
1. Portability
Python is a great option when portability is important. It’s included by default in most Linux distros and supports both Windows and Mac OS. In terms of hardware architecture support, it is on all the platforms that matter. There are the occasional hiccups related to platform specific native lib bindings in 3rd party packages, but they are pretty rare for us embedded folk that work exclusively on Linux.
2. Scripting
Python is the better alternative to Shell or Perl scripts for platform level automation. This is because it’s easier to write, read and debug; not to mention the awesome standard lib features. It’s also great for build system scripting requirements where you would need to tie in package management and build tools together.
3. Productivity
Python produces smaller more concise code. I’ve seen people quoting numbers in the range of Python being 1/3 the size of comparable C++/Java code. Ultimately, it just means that you can get stuff done faster.
4. Math
Python was invented by a mathematician and you can tell. So it should not be a surprise to find that it is the de facto programming language in the Machine Learning and Data Science fields. I will go into a bit more detail on this topic later in this blog post.
领英推荐
5. Integration testing
Python’s ability to inter-op with other languages helps it be the “glue” language of choice for most integration use cases. For embedded systems, a good use case is automated integration testing (i.e. CI), where we would need to mock out I/O like network sockets or serial buses.
6. Code generation
This is a less obvious use case, but bares mentioning. If you have followed good software design patterns and architecture in your code; the chances are that whenever you need to add a new feature, it would involve a lot of boilerplate code writing. For example, in an observer pattern type implementation, you would need to inherit and implement an observer interface and then register your new observer with the observable object. You can automate this kind of skeleton code generation using something like Jinja. Though originally developed for HTML generation in Django; it can be made to work with for C/C++ codegen as well.
7. Speed
Finally we get to the elephant in the room; Python’s biggest deterrent; the fact it uses an interpreter based runtime. There is just no way to get around its higher overhead compared to a native application. In practical terms, the biggest show stopper for Python on embedded systems is the extra memory that is required and NOT the execution speed. This might be surprising to hear for the uninitiated, but in terms of actual execution speed, Python will be very close to native application speed in most compute heavy scenarios. This is possible thanks to Python’s C backend which does the heavy lifting for compute heavy workload. For example, with OpenCV, when you call a method to transform an image; the actual number crunching doesn’t happen in Python bytecode, but in the package’s statically linked C lib implementation. I’ve had to benchmark OpenCV’s Python vs C++ perf on Raspberry Pi a few times in my career and each time Python was able to match the native speed.The gist regarding performance is that if you have the extra RAM to support Python and if the critical sections of your application are either I/O or compute bound; the chances are that you will not be able to measure a performance difference between a Python app vs a native app.
Thanks for reading.