【发布时间】:2020-01-22 14:50:13
【问题描述】:
我正在尝试使用 Python 程序将连续的信息流从一个程序发送到另一个程序,例如 this question,但以字节形式。
pythonPath 和pythonScript 只是脚本和python.exe 的文件位置。
C#代码
public static void PythonKernel_Test() {
Process pythonProcess = Process.Start(new ProcessStartInfo() {
FileName = pythonPath,
Arguments = pythonScript,
RedirectStandardOutput = true,
RedirectStandardInput = true,
UseShellExecute = false,
CreateNoWindow = true
});
Stream pythonStdOut = pythonProcess.StandardOutput.BaseStream;
for (int i = 0; i < 1000; i++) {
byte[] buffer = new byte[256];
pythonStdOut.Read(buffer, 0, 256);
Debug.Log(Encoding.ASCII.GetString(buffer) + Environment.NewLine + BitConverter.ToString(buffer));
}
pythonStdOut.Close();
}
虽然这是在 Unity 中,但您可以将 Debug.Log 替换为 Console.WriteLine()。
但是,即使我用SYNC 向stdout 发送垃圾邮件,C# 端也没有出现任何内容。但是,当从 shell 运行时,它会出现在命令提示符中。
Python 代码
w = sys.stdout.buffer
while True:
w.write(b"SYNC\n")
sys.stdout.flush()
【问题讨论】:
-
为什么不只是
sys.stdout.write(b"SYNC\n")? -
我一直在尝试不同的 IPC 机制,它们都偶然使用
write()作为它们的写入函数,例如管道和套接字。
标签: c# python unity3d process ipc