Pass data from Python to C++

B

brad

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@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@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
 
G

Gary Herron

brad said:
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:


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


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
 
C

Christian Heimes

brad said:
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.


You can either embed the C++ system as a shared library in Python or you
can embed the Python interpreter in your C++ app. Either way you should
use Python's buffer interface to access the data of your library. The
buffer interfaces allows you to create a read only or read/write view on
your data without copying the data.

Christian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top