Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
236 views
in Technique[技术] by (71.8m points)

python - Return output from Runspace Pipeline without waiting for time out

I have implemented a method, which executes the python script.

public static void ExecutePythonScript(string scriptPath,string port,string command)
    {
        string executionCommand = "python "+scriptPath+" "+port+" "+command;
        try
        {
            Runspace runSpace = RunspaceFactory.CreateRunspace();
            runSpace.Open();

            RunspaceInvoke runSpaceInvoker = new RunspaceInvoke(runSpace);
            runSpaceInvoker.Invoke("Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass");

            Pipeline pipeLine = runSpace.CreatePipeline();
            pipeLine.Commands.AddScript(executionCommand);

            Collection<PSObject> returnObjects = pipeLine.Invoke();
            List<string> output = new List<string>();
            foreach (var result in returnObjects)
            {
                output.Add(result.ToString());
            }
            runSpace.Close();
        }
        catch (Exception ex)
        { }
    }

So this works fine. I have an issue. In one of the command there is a timeout sequence for 60 sec.. and returnobjects returns only after the timeout.

here is the command and it output.

 C:UsersccxisyDesktopTest.py COM5 A
 COM Port set to COM5
 Starting Add Mode
 Press Button on Device // I need to display this on ui and should wait for button click//
 got nothing
 GetZWave Timeout!
 error - unable to start program

Since I am not able to read the commands on execution, its exceeding the time out and shows error. Is there a better way that I could display the output simultaneously on execution of each line; instead of after complete execution.

question from:https://stackoverflow.com/questions/66047350/return-output-from-runspace-pipeline-without-waiting-for-time-out

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...