Integrating Python and Threading Building Blocks, Part 1

By Kevin Farnham (107 posts) on January 21, 2008 at 8:16 pm

Several months ago, I began working on integrating Threading Building Blocks with scripting languages, such as Perl and Python. In the past week I've reexamined this problem from a new point of view: focusing on a single scripting language, rather than trying to come up with a single solution that will work for multiple languages.

I'm starting with Python because I've applied it to make graphs of data in the past. The type of application I'd like to create will have a TBB-threaded computation library that will be controlled by a Python user interface, which will also include graphs of the computation results and, where appropriate, of the input data as well.

Interfacing Python and C++

Part of the challenge, of course, is interfacing Python and C++. There are many ways to do this, but after reviewing the alternatives I decided to experiment with the Boost.Python C++ library. Here's the "QuickStart" description for Boost.Python:

The Boost Python Library is a framework for interfacing Python and C++. It allows you to quickly and seamlessly expose C++ classes functions and objects to Python, and vice-versa, using no special tools -- just your C++ compiler. It is designed to wrap C++ interfaces non-intrusively, so that you should not have to change the C++ code at all in order to wrap it, making Boost.Python ideal for exposing 3rd-party libraries to Python. The library's use of advanced metaprogramming techniques simplifies its syntax for users, so that wrapping code takes on the look of a kind of declarative interface definition language (IDL).

That sounds ideal for what I want to do!

Getting Boost

I decided to work on the Windows platform to start out. On Windows, the best way to get Boost is to use the installer provided by Boost Consulting. This is currently available on their Free Downloads page. I chose the lastest version, the BoostPro 1.34.1 Installer.

After clicking on the icon, you agree to the licenses, select a mirror, then select your compiler version. I'm working with Visual Studio 2005 (which includes Visual C++ 8.0). Next, you choose the Boost components you'd like to install. Scrolling down the list, I saw Boost.Python -- quite convenient! Many of the other components (such as Boost Graph, Boost Signals, Boost Wave) look interesting as well; but, I'll have to investigate them at a later date. The "Boost Getting Started on Windows" guide says that Boost.Regex is required to complete their tutorial, so I selected that component in addition to Boost.Python and all the defaults.

I accepted the default folder and let the installation proceed. It took about 20 minutes for the full download (over a DSL connection) and installation to complete. Checking the details, I saw what appeared to be a complete boost\python installation -- at least, a complete set of *.hpp header files for Boost.Python was in place.

Verifying the Boost installation

The Boost Windows Getting Started guide provides some examples for validating that your Boost installation is successful. I followed the steps, building the test applications using a Visual Studio 2005 command prompt, and, once I realized I had to put my Boost paths in quotes, everything worked. My Boost installation was successful.

For reference, here's the statement I used to build the Boost headers-only example.cpp program from within my Visual Studio 2005 command prompt:

cl /EHsc /I "C:\Program Files\boost\boost_1_34_1" example.cpp

And here's my build statement for the Boost Regex example program (which I stored in a file named exampleRegex.cpp):

cl /EHsc /I "C:\Program Files\boost\boost_1_34_1" exampleRegex.cpp ^
/link /LIBPATH:"C:\Program Files\boost\boost_1_34_1\lib"

The caret ^ character is a line continuation indicator.

Next steps

Since this post is already long, I'll pause here. The next step is to work on my Boost.Python installation. Once that's working, I'll try turning one of the TBB example problems into a callable function, and try executing it from and returning results to a Python console application. Finally, I'll construct a Python GUI and data graphing capability.

Kevin Farnham, O'Reilly Media, TBB Open Source Community, Freenode IRC #tbb, TBB Mailing Lists

Download TBB

Categories: Open Source, Parallel Programming, Threading Building Blocks

Comments (0)

Trackbacks (1)


Leave a comment  

To obtain technical support, please go to Software Support.
Name (required)*

Email (required; will not be displayed on this page)*

Your URL (optional)


Comment*