real-time monitoring of propriety system: embedding python in C or embedding C in python?

Duncan Booth duncan.booth at invalid.invalid
Tue Feb 5 16:23:49 CET 2013

Bas <wegwerp at> wrote:

> A) Implement the main program in C. In a loop, get a chunk of data
> using direct call of C functions, convert data to python variables and
> call an embedded python interpreter that runs one iteration of the
> user's algorithm. When the script finishes, you read some variables
> from the interpreter and then call some other C-function to write the
> results. 
> B) Implement the main loop in python. At the beginning of the loop,
> you call an embedded C function to get new data (using ctypes?), make
> the result readable from python (memoryview?), do the user's
> calculation and finally call another C function to write the result. 
> Are there any advantages for using one method over the other? Note
> that I have more experience with python than with C. 

Option B sounds like it makes your life simpler. Just turn the external 
code into a library, use ctypes to call the library and you're done. That 
also means reading command line arguments and/or config files can be done 
in Python and keep the C code simpler.

Embedding Python isn't hard but it sounds more complex than needed here.

You can of course mix the two. If it's more convenient put the main loop in 
Python but use callbacks from the library to handle the values as they 
appear, but again that probably just complicates things.

Duncan Booth

More information about the Python-list mailing list