Communicating Continuously Between Python Script And C App
Solution 1:
There are several possible ways.
You could start the C program from the python program using the subprocess module. In that case you could write from the python program to the standard input of the C program. This is probably the easiest way.
import subprocess
network_data = 'data from the network goes here'
p = subprocess.Popen(['the_C_program', 'optional', 'arguments'],
stdin=subprocess.PIPE)
p.stdin.write(network_data)
N.B. if you want to send data multiple times, then you should not use Popen.communicate().
Alternatively, you could use socket. But you would have to modifiy both programs to be able to do that.
Edit: J.F Sebatian's comment about using a pipe on the command line is very true, I forgot about that! But the abovementioned technique is still useful, because it is one less command to remember and especially if you want to have two-way communication between the python and C programs (in which case you have to add stdout=subprocess.PIPE to the subprocess.Popen invocation and then your python program can read from p.stdout).
Solution 2:
Already mentioned:
- sockets (be careful about framing)
- message queues
New suggestions:
- ctypes (package up the C code as a shared object and call into it from python with ctypes)
- Cython/Pyrex (a Python-like language that allows you to pretty freely mix python and C data)
- SWIG (an interlanguage interfacing system)
In truth, sockets and message queues are probably a safer way to go. But they likely will also mean more code changes.
Post a Comment for "Communicating Continuously Between Python Script And C App"