Pass data from Python to C++
Gary Herron
gherron at islandtraining.com
Thu May 15 15:24:15 EDT 2008
brad wrote:
> I have some c++ binaries that do rather intense number computations.
> They do it well and rather quickly compared to other languages (not
> just Python). An example:
>
>> brad at qu:~/$ date && ./compute.cpp.o < 1_million.txt > /dev/null && date
>> Thu May 15 13:08:28 EDT 2008
>> Thu May 15 13:08:31 EDT 2008
>> brad at qu:~/$ date && python compute.py < 1_million.txt > /dev/null &&
>> date
>> Thu May 15 13:08:38 EDT 2008
>> Thu May 15 13:14:50 EDT 2008
>
> In this case, c++ does one million things in 3 seconds that Python
> takes more than 6 minutes to do. The one million is a minimum. At
> times the computations are in the billions. This is why c++ was chosen.
>
> However, other components can be written in a more user friendly, more
> easily maintained language. We've chosen Python for this. The main
> question now is how to pass the computationally heavy info to c++ from
> within Pyhton. os.system is not ideal. Just wondering how other folks
> do this? I have source to some of the c++ code, but some of it is in
> binary from only. It can take stdin or arguments.
>
> Thanks for any tips,
>
> Brad
>
> --
> http://mail.python.org/mailman/listinfo/python-list
There are lots of ways to do this. Lots of the modules you use are
written in C and callable from Python. (Including sys, os, socket, PIL,
numpy, all graphics and GUI modules, ...) And that's exactly what you
want here -- a module that you can import into Python which gives you
the ability to make calls into your C++ code. This is often called
*wrapping* your C++ library.
This is no small task, and it depends heavily on the size/complexity of
the API you wish to wrap, and whether it's C (easier) or C++(harder).
However, there are *lots* of tools to help. I'd start by looking here:
http://wiki.python.org/moin/AdvocacyWritingTasks/GlueLanguage
Good luck,
Gary Herron
More information about the Python-list
mailing list