Calling a python script and obtaining a return value
I would like to be able to call a python script from within a C program, and then have that python program return a single value back to the C program. One (extremely kludgy) way to do this would be to have C execute a system call to run the python script and then direct the output to a temporary file, and then after returning, have C open up that file and convert the string into a floating point value. But that approach seems to have a lot of rather undesirable characteristics.
I have googled this, and I'm afraid I'm having difficulty understanding what they're suggesting. Does anyone have any suggestions?
On Unix-y systems, you can run your script with one function of the exec() family (for instance, my favourite, execvp()) and then you can use wait() (or waitpid()) to get the exit status (return value) of the child process.
This information can be found in manpages, wikipedia, etc..
Here's an example: C - How can i get return value of command passed to execl? - Stack Overflow
If you want I/we can help you out properly understand exec##() and wait##()
Last edited by Jorl17; 09-21-2010 at 12:59 PM.
Thanks for the reply. As far as I know (which is admittedly limited), return values are limited to between -255 to 255, and the value being returned will be a float- and I won't have nearly enough precision to return the value as an unsigned short int and then convert it back into a float. I know that there is a way to run a python script natively within a C program, and that way I can both pass it parameter values and get the exact return value from one of it's internal functions.
Am I making any sense?
I get what you mean. Maybe some form of IPC?
If not, check this (old) article, it may be useful:
Embedding Python in Your C Programs | Linux Journal
edit: It's actually 0-255, which are all the non-negative values that a byte can contain. I doubt anything can range from -255 to 255, that'd mean that said datatype would be able to contain 511 different values. Well, 511 isn't a power of two... I don't think I'm wrong in this one.
Last edited by Jorl17; 09-22-2010 at 10:50 AM.