Embedding Python in C++ Applications with boost::python: Introduction

Posted on 02 January 2012 by Joseph

About a year ago, we at MiserWare decided to augment the core power management function of Granola with web connectivity, allowing users to track the savings of all of their machines (and soon, to configure and apply policies and schedules) from a single location – the Granola Dash.

The problem was, though, that our codebase was entirely in C++. I examined several options. Ultimately, I decided that writing the web connectivity code in Python and embedding it in Granola would give me the best agility for my buck. I found boost::python and used it as the (excellent) basis of my implementation.

As the months have gone on, I have improved my understanding and implementation of embedded Python in this context, and I have increasingly reached for it to solve all sorts of problems that are painful in C++ and painless in Python – configuration parsing, complex data structures marshaled in JSON, automatic updating, and basically anything else that isn’t core algorithms (for performance reasons) or system interaction (for compatibility).

Here were my initial requirements:

  • instantiate Python objects and interact with them in a natural way
  • pass data into Python functions
  • extract data from Python functions and objects
  • handle errors from with the Python code

After the code started getting more sophisticated, I realized the following were also important topics:

  • call Python code from multiple (actual) threads of execution
  • parse Python exceptions into usable data structures

This series of tutorials is my attempt to document my experiences and help out others who want to take advantage of Python in their C++ applications. In Part 1, I’ll cover the basics of embedding Python and using boost::python, and outline a simple C++/Python application. Afterwards, I’ll cover the topics above and provide some code to solve a lot of the problems that I struggled with initially.

Brief Bio

Posted on 01 January 2012 by Joseph

I am currently the VP of Product at Mobile System 7, where I work with customers, sales, engineering, and business leadership to design and build Interlock, the most powerful and comprehensive Identity Analytics and Adaptive Access Control platform in the world. I gather requirements, prioritize development resources, help structure marketing, and work with the sales team to improve our processes and our product. I also lend a hand with development.

Previously, I co-founded and led the technical side of MiserWare from 2007 until 2013. At MiserWare, we produced Granola, the only commercially available software that offered dramatic reduction in computer power consumption within a hard performance constraint. We extended this technological core to create an enterprise scale software suite for managing the energy of IT resources. Unfortunately, in 2013 MiserWare shut down due to lack of funding.

Many years ago, I was a PhD student and a Cunningham Fellow at Virginia Tech, though I left the program to found MiserWare. I graduated Magna Cum Laude from the University of South Carolina in December 2008. I was a member of the SCAPE Lab from Fall 2004, first at USC, then at Virginia Tech, prior to leaving in 2007.

In my spare time, I rock climb whenever weather and time permits; take poor photographs; write, play, and record music; and do a whole bunch of recreational math and programming.

Professional Statement

My general interests lay with software entrepreneurship. Creating engaging products and communicating their value to customers has never been easier or more bootstrappable than it is right now. Since I was a kid, I have been thrilled by computers and computing; combining that with the challenges and thrills (not to mention the sometimes brutal economics) of a business has been a fascinating path of discovery for me. Times aren’t always easy, but they are rarely boring.

In my role at Mobile System 7, I am striving to discover the most efficient way to create market fit within the bounds of high-tech, resource intensive software development and often slow enterprise sales cycles. I have been experimenting with ideas from The Lean Startup and the micro ISV community, coupling the fast feedback loops they recommend with lessons learned about enterprise sales at MiserWare to rapidly improve not only our product but also our sales process.

In addition to my role in founding and helping shape Miserware, I either was or led the engineering side of the company from its inception, and this has given me the opportunity to work on more aspects of software and computing than I can count. Roughly in chronological order, and leaving out the small stuff: designing and implementing core algorithms, designing software architectures, test-driven development, interviewing and hiring, choosing and building a team, agile development, project management, benchmarking, creating datacenter-scale technologies, timeline and release management, creating user interfaces, creating and facilitating cross-platform software, software maintenance and updating, and creating a disruptive next-generation have all been on my plate at one point or another. Being in the drivers seat for a nascent company is a sure way to get your hands dirty with all kinds of different dirt.

Copyright © 2018 Joseph Turner