C#和Python之间的进程间通信 [英] Interprocess communication between C# and Python

查看:1024
本文介绍了C#和Python之间的进程间通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以理解,有关此主题的问题很多,但没有一个能够真正解决我的问题.因此,在这里我介绍了我的代码,并希望在这里指出我的错误.

I can understand that there has been a lot of questions on this topic but none of them could really solve my problem. So here I have presented my code, and I want my mistakes to be pointed out here.

我有一个用C#编写的程序,该程序将调用python可执行文件/文件.第一个要求是我已经通过输入流将一个参数传递给python文件.我能做的.我现在面临的真正问题是,我必须查看我的python文件是否正在打印"Please enter arguments_x",我必须在C#代码中读取此输出,并检查它是否为arguments_x,然后仅将参数值写入输入流.下面是C#和Python的代码段.

I have a program written in C# which shall call a python executable/file. The first requirement is that I have pass one argument to the python file via the input stream. This I could do. The real problem I am facing now is that, I have to see whether my python file is printing "Please enter argument_x", I have to read this output in my C# code and check if it is argument_x, then only write the argument value in the input stream. Below are the code snippets for C# and Python.

C#代码如下:

using System;
using System.IO;
using System.Diagnostics;
using System.ComponentModel;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a new process object
            Process myProcess = new Process();

            //Provide the start information for the process
            myProcess.StartInfo.FileName = "python.exe";
            myProcess.StartInfo.Arguments = "mytestpython.py";
            myProcess.StartInfo.UseShellExecute = false;
            myProcess.StartInfo.RedirectStandardInput = true;
            myProcess.StartInfo.RedirectStandardOutput = true;

            StreamReader myStreamReader;
            StreamWriter myStreamWriter;

            //Invoke the process from current process
            myProcess.Start();

            myStreamReader = myProcess.StandardOutput;

            //Read the standard output of the spawned process.
            string myString = myProcess.StandardOutput.ReadToEnd();
            Console.WriteLine(myString);

            if (myString.Contains("argument_x"))
            {
                myStreamWriter = myProcess.StandardInput;
                String argument = "argument_value";
                myStreamWriter.WriteLine(argument);
            }

           myProcess.WaitForExit();
           myStreamWriter.Close();
           myStreamReader.Close();
           myProcess.Close();
      }
   }
}

mytestpython.py 文件中的python程序如下所示:

The python program in mytestpython.py file looks like this:

import sys
import getpass
prompt_string = "Please enter argument_x"
if sys.stdin.isatty():
    reqd_arg = getpass.getpass(prompt=prompt_string)
else:
    print(prompt_string)
    reqd_arg = sys.stdin.readline().rstrip()

请帮帮我,因为我觉得我正确地编写了90%的代码,中间之间有一个小错误.预先感谢您的帮助.

Please help me out, as I feel I have written 90% of the code correctly with a minor mistake somewhere in between. Thanks in advance for your help.

推荐答案

当您执行myProcess.StandardOutput.ReadToEnd();时,它将尝试读取到Python程序的stdout的末尾,这意味着它将等待Python程序完成执行并关闭其stdout流,因为它正在等待C#程序的输入,所以它从未执行过此操作.这会导致死锁.

When you do myProcess.StandardOutput.ReadToEnd();, it tries to read to the end of the stdout of your Python program, meaning it will wait for the Python program to finish executing and close its stdout stream, which it never does because it's waiting for input from your C# program. This results in a deadlock.

这篇关于C#和Python之间的进程间通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆