Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在VB.net中运行外部进程时使用sleep_Python_Vb.net - Fatal编程技术网

Python 在VB.net中运行外部进程时使用sleep

Python 在VB.net中运行外部进程时使用sleep,python,vb.net,Python,Vb.net,我在VB.net中运行Python脚本时遇到问题,因为.net程序会占用大量CPU。本质上,我在VB.net程序中执行Python脚本,重新定向标准输出,以便Python脚本打印的内容被.net捕获 Dim python_handler As New PythonHandler python_handler.Execute("python.exe", "my_script.py") ' Wait f

我在VB.net中运行Python脚本时遇到问题,因为.net程序会占用大量CPU。本质上,我在VB.net程序中执行Python脚本,重新定向标准输出,以便Python脚本打印的内容被.net捕获

                Dim python_handler As New PythonHandler
                python_handler.Execute("python.exe", "my_script.py")

                ' Wait for python_handler to get back data
                While python_handler.pythonOutput = String.Empty

                End While

                Dim pythonOutput As String = python_handler.pythonOutput
PythonHandler是一个类,其执行函数如下所示:

Public Sub Execute(ByVal filePath As String, ByVal arguments As String)
    If _process IsNot Nothing Then
        Throw New Exception("Already watching process")
    End If
    _process = New Process()
    _process.StartInfo.FileName = filePath
    _process.StartInfo.Arguments = arguments
    _process.StartInfo.UseShellExecute = False
    _process.StartInfo.RedirectStandardInput = True
    _process.StartInfo.RedirectStandardOutput = True
    _process.Start()
    _process.BeginOutputReadLine()
End Sub

Private Sub _process_OutputDataReceived(ByVal sender As Object, ByVal e As System.Diagnostics.DataReceivedEventArgs) Handles _process.OutputDataReceived
    If _process.HasExited Then
        _process.Dispose()
        _process = Nothing
    End If
    RaiseEvent OutputRead(e.Data)
End Sub

Private Sub textProcessing_OutputRead(ByVal output As String) Handles Me.OutputRead
    outputFetched = True
    pythonOutput = output
End Sub
问题在于While循环的存在,因为它等待Python脚本完成。CPU运行率高达100%。我尝试将System.Threading.Thread.Sleep(200)放入While循环,但是.net程序未能捕获Python输出,没有返回任何内容。可能是因为Process.BeginOutputReadLine()是异步的吗


谢谢。

我看不到将
\u process\u OutputDataReceived
分配给
\u process.OutputDataReceived
事件的位置。您应该在启动进程之前分配它,然后调用
\u process.WaitForExit()
,而不是调用while循环。它应该自动阻止,直到进程完成。至少我的C#test做到了

_process = New Process()
_process.StartInfo.FileName = filePath
_process.StartInfo.Arguments = arguments
_process.StartInfo.UseShellExecute = False
_process.StartInfo.RedirectStandardInput = True
_process.StartInfo.RedirectStandardOutput = True
AddHandler _process.OutputDataReceived, AddressOf _process_OutputDataReceived
_process.Start()
_process.BeginOutputReadLine()
_process.WaitForExit()

要扩展到狡猾的皮特的答案。。。每当您使用异步方法时,使用轮询循环等待结果几乎是不正确的。下面这一行是非常处理器密集型的,并且破坏了使用异步方法的性能优势

While python_handler.pythonOutput = String.Empty
End While
如果您发现自己在使用轮询循环,您应该问问自己是否有一种事件驱动的方法来处理此问题,或者在这种情况下,是否有一种正确的方法来等待事件

_process.WaitForExit()

PythonHandler的类内定义:Private with events\u process As process。子进程OutputDataReceived在函数头中具有句柄process.OutputDataReceived。我会试试你的建议!我已经做了您建议的更改,在一些测试之后,我注意到OutPutDataReceived事件被调用了两次,一次是使用空字符串。我会做更多的调整。嗨,因为OutputDataReceived事件会引发两次,我发现我必须使用While循环来检查进程何时退出。在OutputDataReceived处理程序中,我有一个检查,检查进程是否已退出,如果已退出,我将布尔变量设置为True。但这意味着OutputDataReceived会在进程退出之前被调用,因为它执行了两次,而在整个进程中只有第二次被退出。您好,我可能已经找到了更好的解决方案。我没有在Sleep中使用While循环,而是将AutoResetEvent对象传递给pythonnandler类。PythonHandler类在事件完成时发出信号,调用线程可以继续。Dim resetEvent作为新线程。AutoResetEvent(False)Dim python_处理程序作为新的PythonHandler(resetEvent)python_处理程序。Execute(“python.exe”、“my_script.py”)resetEvent.WaitOne()会阻止当前执行线程,直到WaitHandle收到信号。谢谢您的建议,非常有用!您好,我用事件驱动的方式(使用Threading.AutoResetEvent)替换了thw While循环。