在标准输出中没有捕获到某些Python命令

问题描述:

我已经编写了一个捕获并执行命令行Python脚本的简单程序,但有一个问题。传递给Python输入函数的文本不会写入我的程序,尽管我的程序捕获了stdout。在标准输出中没有捕获到某些Python命令

例如: 的Python脚本:

import sys 

print("Hello, World!") 
x = input("Please enter a number: ") 
print(x) 

print("This work?") 

还会写 “你好,世界!”然后停下来。当我通过一个数字时,它会继续写下“请输入一个数字:3”。到底是怎么回事?任何解决方案我的C#如下:

public partial class PyCon : Window 
{ 
     public string strPythonPath; 
     public string strFile; 
     public string strArguments; 
     private StreamWriter sw; 

     public PyCon(string pythonpath, string file, string args) 
     { 
      strPythonPath = pythonpath; 
      strFile = file; 
      strArguments = args; 

      InitializeComponent(); 

      Process p = new Process(); 

      p.StartInfo.FileName = strPythonPath; 
      p.StartInfo.Arguments = "\"" + strFile + "\" " + strArguments; 

      p.StartInfo.UseShellExecute = false; 
      p.StartInfo.CreateNoWindow = true; 

      p.StartInfo.RedirectStandardInput = true; 
      p.StartInfo.RedirectStandardOutput = true; 
      p.StartInfo.RedirectStandardError = true; 

      p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived); 
      p.ErrorDataReceived += new DataReceivedEventHandler(p_ErrorDataReceived); 

      p.Start(); 
      p.BeginOutputReadLine(); 
      p.BeginErrorReadLine(); 
      sw = p.StandardInput; 
     } 

     private void p_OutputDataReceived(object sendingProcess, DataReceivedEventArgs received) { 
      if (!String.IsNullOrEmpty(received.Data)) { 
       AppendConsole(received.Data); 
      } 
     } 

     private void p_ErrorDataReceived(object sendingProcess, DataReceivedEventArgs received) { 
      if (!String.IsNullOrEmpty(received.Data)) { 
       AppendConsole(received.Data); 
      } 
     } 

     private void AppendConsole(string message) { 
      if (!txtConsole.Dispatcher.CheckAccess()) { 
       txtConsole.Dispatcher.Invoke(DispatcherPriority.Normal, (System.Windows.Forms.MethodInvoker)delegate() { txtConsole.AppendText(message + "\n"); }); 
      } else { 
       //Format text 
       message = message.Replace("\n", Environment.NewLine); 

       txtConsole.AppendText(message + "\n"); 
      } 
     } 

     private void txtInput_KeyUp(object sender, KeyEventArgs e) { 
      if (e.Key != Key.Enter) return; 

      sw.WriteLine(txtInput.Text); 

      txtInput.Text = ""; 


     } 
    } 

编辑:大量的研究后,并从该线程的帮助,我得出的结论是,问题是用Python的输入命令不调用C#DataReceivedEventHandler。除了脚本更改外,可能还没有解决方案。如果是这种情况,我会做出答案,包含所接受的更改。感谢您的帮助,伙计们!

像Python I/O这样的气味是行缓冲的,即等待CRLF,然后一次发送整行。您可以尝试打开该关闭(蟒蛇-u myscript.py,或设置PYTHONUNBUFFERED环境变量)与这样的事情或解决它:

print("Hello, World!") 
print("Please enter a number: ") 
x = input() 
print(x) 
+0

该脚本的解决方法是伟大的。它纠正了这个问题,但并不理想。 设置PYTHONUNBUFFERED或使用-u运行脚本似乎无法解决问题。 – 2010-02-23 22:25:28

这很难说,因为我使用蟒蛇2.6和您似乎使用3.x中,我也没有在C#编程很长一段时间,但我的猜测是,这条线:

sw.WriteLine(txtInput.Text); 

正在发送“3”加一个窗口换行字符。

尝试:

sw.Write(txtInput.Text + "\n") 
sw.Flush() 

这将只发送一个正常的newline,而不是一个窗口回车这可能是问题。在处理像这样复杂的流通信时,请确保您总是Flush!

还有一件事,确保你可以在命令提示符下重定向它。往往程序员试图在同一时间做的一切,并卡住:

./stdintest.py < input.txt 

如果没有在那里工作,它不会在C#中工作。祝你好运

+0

需要注意的是,如果我在input.txt中进行操作,而不是放置正常的换行符,则在3之后放置一个^ M(windows字符),python 2.6会在解析时抛出异常“意外的EOF”。 python 3.x – manifest 2010-02-23 22:27:51

+0

这可能也可能不是这种情况刷新StreamWriter绝对是一个很好的调用。然而,换行符处理本质上与我相反的问题。输入的帖子很好,但传入Python输入命令的字符串不会显示在我的捕获数据中。所以运行该程序就像这样: Hello,World! 3 [ 2010-02-24 02:28:59

+0

我切换到版本2.6。我看到你指的错误。它在3.1中似乎不是问题。 – 2010-02-24 07:01:57