简体   繁体   English

文本文件可以同时由两个程序更新(实时通信)吗?

[英]Can a text file be updated by two programs simultaneously (live communication)?

I have a program that has c/c# abilities, and I have python. 我有一个具有c / c#能力的程序,我有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. 我希望该程序几乎以毫秒为单位更新文本文件,并让python以毫秒为单位读取该文本文件。 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? 是否可以通过另一个程序实时更新文本文件并通过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? 基本上我想做的是使用python对来自该程序的实时数据进行大量计算,并以命令的形式将这些计算发送回程序。可以在内存中关闭并重新打开并更新文件吗?

If you start the C/C# process from python with subprocess.Popen then your two programs can communicate via the stdin and stdout pipes: 如果你使用subprocess.Popen从python启动C / C#进程,那么你的两个程序可以通过stdinstdout管道进行通信:

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. 另请注意,在python 3中这些将返回bytes对象,您可以使用.decode()方法转换为str

Then to send input to the process you can simply write to the stdin: 然后将输入发送到进程,您只需写入stdin:

c_program.stdin.write(DATA)

Like the read above, in python 3 this method expects a bytes object. 与上面的read一样,在python 3中,此方法需要一个bytes对象。 You can use the str.encode method to encode it before writing it to the pipe. 在将其写入管道之前,您可以使用str.encode方法对其进行编码。


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. 我对C#的知识非常有限,但是从有限的研究来看,你似乎可以System.Console.In读取数据并将数据写入System.Console.Out ,尽管如果你在C#中编写了在终端中运行的程序,那么用于将数据写入屏幕并从用户读取输入的方法也适用于此处。 (you can imagine the .stdout as the terminal screen and data python writes to .stdin the user input) (你可以想象.stdout作为终端屏幕和数据python写入.stdin用户输入)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM