小编Kev*_*vin的帖子

在给定条件不良的情况下使用异常在执行过程中停止IO.popen

在我的Ruby脚本中,我调用Perl脚本并等待它完成执行.但是,有时候Perl脚本会遇到一系列错误,我希望Ruby能够自动处理这些错误.所以,我实现了以下......

begin
    IO.popen(cmdLineExecution) do |stream|
          stream.each do |line|
              puts line
              if line =~ /Some line that I know is an error/
                 raise MyOwnException
              end 
          end
    end

    begin
          #Wait on the child process
          Process.waitpid 
    rescue Errno::ECHILD
    end
rescue MyOwnException
   #Abort the command mid processing, and handle the error
end
Run Code Online (Sandbox Code Playgroud)

但是,即使抛出异常,Perl脚本仍继续执行,只是它STDOUT不再输出输出.此时,如果我想停止Perl进程,我必须进入任务管理器并手动停止它.然后Process.waitpid结束并从那里继续.或者我停止Ruby并且Perl进程继续在后台运行,我仍然需要手动停止它.

顺便说一句:这是在Windows上

因此,问题是如何在没有Perl进程成为孤立进程中间进程的情况下如何停止IO.popen?

ruby

1
推荐指数
1
解决办法
1167
查看次数

标签 统计

ruby ×1