1

I have a program that has c/c# abilities, and I have python. I want that program to update a text file, almost in milliseconds, and have the python to read that text file in milliseconds as well. How can I go achieve this?

Is it possible for a text file to be updated live by another program and be read live by python? Is there any alternative way to do this instead of relying on text file. Basically what I want to do is a bunch of computations on live data from that program using python and send back those computations to the program in form of commands.Can a file not be closed and reopened and yet updated in the memory?

alk
  • 69,737
  • 10
  • 105
  • 255
auryndb
  • 165
  • 1
  • 2
  • 12
  • 2
    I think you want a Pipe instead of a file. – Tadhg McDonald-Jensen Apr 25 '16 at 22:50
  • 2
    Adding to what @TadhgMcDonald-Jensen said, http://stackoverflow.com/questions/6977561/pipe-vs-temporary-file – RamblinRose Apr 25 '16 at 22:52
  • 1
    I don't think you can achieve "almost" part - the changes will be visible immediately... (Read on share mode for opening files if you really want to go with text file approach - i.e. http://stackoverflow.com/questions/222089/in-c-if-2-processes-are-reading-and-writing-to-the-same-file-what-is-the-best) – Alexei Levenkov Apr 25 '16 at 22:52
  • @AlexeiLevenkov I edited the question. – auryndb Apr 25 '16 at 22:57
  • 1
    I know that you can establish the pipes if the C part is run from python via a [`subprocess.Popen`](https://docs.python.org/3.5/library/subprocess.html#subprocess.Popen) just by specifying `stdout=subprocess.PIPE` (and same for `stdin` and `stderr`) and then you can `.communicate` with it. – Tadhg McDonald-Jensen Apr 25 '16 at 22:58
  • @TadhgMcDonald-Jensen very helpful. a quick search brings up Named Pipes? does it beg its own question or can Named pipes work for my work too? – auryndb Apr 25 '16 at 22:59
  • 1
    is it worthy of an answer? I have no idea how the communications would work from the C# end... Well the same way you would write to the standard out / read from standard in.. – Tadhg McDonald-Jensen Apr 25 '16 at 23:00
  • @TadhgMcDonald-Jensen I don't know. I just started reading about pipes after your suggestion, probably will take me a couple of hours to know what is going on and if it suits what I want to do. We'll see. – auryndb Apr 25 '16 at 23:02
  • 1
    I looked at https://msdn.microsoft.com/en-us/library/system.console(v=vs.110).aspx#Anchor_2 and the C# communications look like it would be handled via `System.Console.In` and `System.Console.Out` – Tadhg McDonald-Jensen Apr 25 '16 at 23:06

1 Answers1

0

If you start the C/C# process from python with subprocess.Popen then your two programs can communicate via the stdin and stdout pipes:

c_program = subprocess.Popen(["ARGS","HERE"],
                             stdin = subprocess.PIPE,  # PIPE is actually just -1
                             stdout= subprocess.PIPE,  # it indicates to create a new pipe
                             stderr= subprocess.PIPE  #not necessary but useful
                             )

Then you can read the output of the process with:

data = c_program.stdout.read(n) #read n bytes
#or read until newine
line = c_program.stdout.readline()

Note that both of these are blocking methods, although non blocking alternatives exist.

Also note that in python 3 these will return bytes objects, you can convert into a str with the .decode() method.

Then to send input to the process you can simply write to the stdin:

c_program.stdin.write(DATA)

Like the read above, in python 3 this method expects a bytes object. You can use the str.encode method to encode it before writing it to the pipe.


I have extremely limited knowledge of C# but from limited research it seems that you can read data from System.Console.In and write data to System.Console.Out, although if you have written programs in C# that run in a terminal, the same methods used to write data to the screen and read input from the user will work here too. (you can imagine the .stdout as the terminal screen and data python writes to .stdin the user input)

Community
  • 1
  • 1
Tadhg McDonald-Jensen
  • 20,699
  • 5
  • 35
  • 59