如何在C#流和Python脚本之间重复传递输入和输出

本文关键字:输入 输出 之间 流和 脚本 Python | 更新日期: 2023-09-27 18:30:02

我正试图从C#运行一个Python脚本作为流,并使用StreamWriter和StreamReader在Python和流之间重复传递输入和输出。

我会读写,但显然只有一次,而不是多次。(这正是我需要的。)希望有人能告诉我我做错了什么。

(我知道我可能可以通过读写文件来做我需要做的事情。然而,如果可以的话,我想避免这种情况,因为使用Stream似乎更干净。)

这是我的C#代码:

using System;
using System.Diagnostics;
using System.IO;
public class Stream_Read_Write
{
    public static void Main()
    {
        string path = "C:''Users''thomas''Documents''Python_Scripts''io_test.py";
        string iter = "3";
        string input = "Hello!";
        stream_read_write(path, iter, input);
        //Keep Console Open for Debug
        Console.Write("end");
        Console.ReadKey();
    }
    private static void stream_read_write(string path, string iter, string input)
    {
        ProcessStartInfo start = new ProcessStartInfo();
        start.FileName = "C:''Python27''python.exe";
        start.Arguments = string.Format("{0} {1}", path, iter);
        start.UseShellExecute = false;
        start.RedirectStandardOutput = true;
        start.RedirectStandardInput = true;
        start.CreateNoWindow = true;
        using (Process process = Process.Start(start))
        using (StreamWriter writer = process.StandardInput)
        using (StreamReader reader = process.StandardOutput)
        {
            for (int i = 0; i < Convert.ToInt32(iter); i++)
            {
                Console.WriteLine("writing...");
                writer.WriteLine(input);
                writer.Flush();
                Console.WriteLine("written: " + input + "'n");
                Console.WriteLine("reading...");
                string result = reader.ReadLine();
                Console.WriteLine("read: " + result + "'n");
            }
        }
    }
}

Python代码如下:

import sys
iter = int(sys.argv[1])
for i in range(iter):
    input = raw_input()
    print (input)

这是我得到的输出:

writing...
written: Hello!
reading...

奇怪的是,当我从Python和C#中删除循环时,它就起作用了。(对于一次迭代)

writing...
written: Hello!
reading...
read: Hello!
end

我不清楚为什么会发生这种情况,也不清楚解决方案是什么,所以我们非常感谢任何帮助。

如何在C#流和Python脚本之间重复传递输入和输出

我找到了解决问题的方法。不过,我真的不知道为什么会这样。对于C#:

using System;
using System.Diagnostics;
using System.IO;
public class Stream_Read_Write
{
    public static void Main()
    {
        string path = "C:''Users''thomas_wortmann''Documents''Python_Scripts''io_test.py";
        string iter = "3";
        string input = "Hello";
        stream_read_write(path, iter, input);
        //Keep Console Open for Debug
        Console.Write("end");
        Console.ReadKey();
    }
    private static void stream_read_write(string path, string iter, string input)
    {
        ProcessStartInfo start = new ProcessStartInfo();
        start.FileName = "C:''Python27''python.exe";
        start.Arguments = string.Format("{0} {1}", path, iter);
        start.UseShellExecute = false;
        start.RedirectStandardOutput = true;
        start.RedirectStandardInput = true;
        start.CreateNoWindow = true;
        using (Process process = Process.Start(start))
        using (StreamWriter writer = process.StandardInput)
        using (StreamReader reader = process.StandardOutput)
        {
            for (int i = 0; i < Convert.ToInt32(iter); i++)
            {
                writer.WriteLine(input + i);
                Console.WriteLine("written: " + input + i);
                string result = null;
                while (result == null || result.Length == 0)
                { result = reader.ReadLine(); }
                Console.WriteLine("read: " + result + "'n");
            }
        }
    }
}

python代码如下:

import sys
def reverse(input):
    return input [::-1]
iter = int(sys.argv[1])
for i in range(iter):
    input = sys.stdin.readline()
    print reverse(input)
    sys.stdout.flush()

这是一个传统的流问题。你必须"冲洗"溪流(例如stdout.flush(),其中stdout是流对象)以便发送数据无论是在C#中还是在Python中。正如我所知,在C#中,您必须执行流。Close()为了完成冲洗本身,或者你可以用"using"来包装流并且它在括号闭合时发送数据。

编辑:顺便说一句,流一次只对一侧可用。