Posted on March 10, 2014
At x20x we deal with a lot of APIs, and even more data. And while python is usually fast enough, there are parts that require blazing fast performance. This is where a programmer is left with couple options:
– Optimize python routines
– Rewrite parts of your code to Cython
– Write the required code in C/C++ and expose this functionality to python.
Generally speaking, optimizing python routines is the most important step but it will also get you only to a certain point after which you cannot improve it anymore. When that is not enough you have to either write the parts in Cython or write them in C/C++ and then include them inside your Python. I personally dislike Cython (reasons won’t be included in this article); so I strongly lean toward C++ extensions. That also comes with multiple options, but in this article I will explain the easiest, and my favorite, way to do it – with Boost.Python.
We should start by writing the crucial part of the code in C++ and make sure that it works. For this tutorial, we will use API client for http://friendlyproxies.com/. We use them at X20X to scrap google SERPS (they are awesome, make sure to check them out if you are in need of heavy googling without hassles of getting banned and rotating long proxy list) and you can find the code in this handy gist (it isn’t the production code we use, but it’s much more readable than plain C mess we use): https://gist.github.com/Puciek/9465889#file-client-cpp. Download it and fire it up in your favorite editor. And with that, let’s go over the code. First we define a function to handle compression.
Which takes a ZLIB compressed std::string as input and returns uncompressed one by leveraging Zlib.h.
Next we have our function of interest:
Which takes a lot of arguments, builds a json request based on them, sends it to the API endpoint, reads the data back from socket and returns uncompressed result. It’s pretty basic example of how to build a TCP client with Boost, nothing special in here.
Now we have all the logic we need to operate with API, all is left is to expose it to python. Luckily enough, it’s very easy to do with Boost! All you have to do is to include boost/python.hpp into your application and then it’s a matter of very simple and self-explanatory piece of code:
And if we wanted to expose a class, it is almost as easy:
Now, with the code ready, we have to compile and link it, so let’s get to it. Make sure that you got jsoncpp, boost_system and boost_python installed; otherwise, you will not have a good time.
The -I argument and following path is not required if your g++ is running on more liberal settings. I prefer to have longer compile commands, and thus impose tighter control over what is going in. If you want to build the code against another version of python, just change the path accordingly. Assuming that no error shows up (if there are some, try upgrading GCC to 4.6/4.7 branch), we have successfully compiled our client and all left for there is to link it all together.
Make sure that boost_python and the same version which you used for compilation is included, otherwise you will run into missing symbols or segfaults.
Now with code compiled and linked into a handy .so, all there is left is to test it out:
That looks like a success, we’ve queries the friendlyproxies API and received a serp which weights 270422 bytes. Sounds about right!
And that concludes this very basic introduction to exposing C++ functionality into Python. There is much more which Boost offers in that department and you can read about it in the documentation. And as always, remember to subscribe for more of the good, and leave your questions, flames, ideas and thoughts in the comment section below.