Wednesday, January 18, 2006

How Python and C/C++ Work Together?

Recently, I have had a chance to work on integrating Python modules with C/C++ applications. Python is a powerful interpreted (and scripting) language, like Perl and PHP. Along with the available tools and libraries, Python makes a good choice for modeling and simulation developers. Best of all, it's free and the tools written for Python programmers are also free. For more details on the language, visit the "official website".

Now, you have got some modules written in Python by others and you want to use them in C/C++ applications. You are experienced in C/C++, but fairly new to Python. You might wonder if you can find a tool to convert them to C code, like the conversion from FORTRAN. The answer is no. The only choice you're having is to work on bringing Python and C/C++ together.

There are two general approaches to integrating Python with C/C++. One is embedding, in which you use Python/C API library to call Python modules directly, as you call any other C/C++ functions. I'm not going to say more about this approach. For more details, see my articles on "CodeProject".

In contrst, the second approach allows Python and C/C++ modules to run separately and talk to each other through an IPC mechanism. This really is a "black-box" approach, in which your C/C++ code does not call Python modules at all. Instead, they communicate with each other through message exchanges.

The IPC approach requires that you convert your Python modules into executables and libraries so that you can run them as independant applications. There are several such tools that can help, one of which is called "cx_Freeze", which is portable across platforms. I've tested it on Windows XP, RedHat Linux and SGI Irix 64. When implementing this approach, you need to do two things:

1) Write IPC interfaces for both Python and C/C++ modules. I found it fairly simple to implement a memory-mapped files (MMAP) IPC on both Python and C/C++ sides.

2) Convert each Python module to an executable with associated libraries. Then you are ready to run Python and C/C++ modules as separate processes and let them talk to each other through the IPC.

Personally, I prefer the first approach as it gives us a "white-box" control over Python modules. One drawback about the first approach is that it demands more intimate knowledge of the Python implementation.


Blogger Coldfire said...

"Write IPC interfaces for both Python and C/C++ modules"

Could you point me to some place to learn how to write this IPC thing.

8:19 PM  
Blogger Jun Du said...

With C/C++ or Python?

5:12 PM  
Blogger alex said...

Hello Jun, i have application with embedded python with sub-interpreter staff used....but it crashes sometime....i have a py script example which crashes my app, can you help me with that? i can provide you with teamviewer access to my pc with ready to use debug environment...i have have not enough experience with embedding python and can`t solve this can i contact you to speak about this?

5:24 PM  

Post a Comment

<< Home