1

我正在运行一个带有后台工作人员的 Windows 窗体,以根据 python 脚本的输出更新文本框。除了重定向的输出不是实时的之外,一切都运行得很好;它的延迟相当大。

有什么想法可以增加重定向输出的响应时间吗?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;
using System.IO;

namespace JiraHeartBeat
{
    public partial class Form1 : Form
    {

        delegate void AppendTextDelegate(string text);
        BackgroundWorker Worker = new BackgroundWorker();

        public Form1()
        {
            InitializeComponent();

            Worker.DoWork += new DoWorkEventHandler(Worker_DoWork);
            Worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(Worker_RunWorkerCompleted);

        }

        void Worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            StartButton.PerformClick();
        }

        private void StartButton_Click(object sender, EventArgs e)
        {
            if (!Worker.IsBusy)
            {
                Worker.RunWorkerAsync();
            }
        }

        public void Worker_DoWork(object sender, DoWorkEventArgs e)
        {
            Process pro = new Process();
            pro.StartInfo.RedirectStandardOutput = true;
            pro.StartInfo.RedirectStandardError = true;
            pro.StartInfo.UseShellExecute = false;
            pro.StartInfo.CreateNoWindow = true;
            pro.EnableRaisingEvents = true;
            pro.OutputDataReceived += new DataReceivedEventHandler(OnDataReceived);
            pro.ErrorDataReceived += new DataReceivedEventHandler(OnDataReceived);

            pro.StartInfo.FileName = "C:\\Python27\\python.exe";
            pro.StartInfo.Arguments = "\"C:\\Python27\\myscript.py\"";

            try
            {
                pro.Start();
                pro.BeginOutputReadLine();
                pro.BeginErrorReadLine();
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            Thread.Sleep(5000 * 60);
        }


        public void OnDataReceived(object sender, DataReceivedEventArgs e)
        {

            if (e.Data != null)
            {
                string temp = (e.Data) + Environment.NewLine;
                appendText(temp);

            }
        }
        public void appendText(string text)
        {
            if (ResultTextBox.InvokeRequired)
            {
                ResultTextBox.Invoke(new AppendTextDelegate(appendText), new object[] { text });
            }
            else
            {
                ResultTextBox.AppendText(text);
            }
        }
    }
}
4

2 回答 2

1

实际上,问题在于 Python 在脚本完成之前不会重定向输出。我相信 IronPython 会在脚本运行时重定向(虽然尚未对此进行测试),但不幸的是,常规 Python 必须等待脚本结束才能重定向输出。

于 2013-11-05T22:32:47.117 回答
0

尝试从 中删除以下行Worker_DoWork,我怀疑它会延迟RunWorkerCompleted事件的执行。

Thread.Sleep(5000 * 60);

编辑

由于尝试了上述方法并且没有完全解决问题,我进一步调查并确认从 python 脚本捕获输出时响应被延迟。但是,通过添加对我的调用,sys.stdout.flush()我能够获得所需的行为。这是我在测试中成功使用的 python 脚本。

import time
import sys

for x in xrange(0,11):
    print x
    time.sleep(1)
    sys.stdout.flush()
于 2013-10-31T03:28:15.463 回答