如何等待进程完成使用IO.popen? [英] How to wait for process to finish using IO.popen?

查看:177
本文介绍了如何等待进程完成使用IO.popen?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Ruby中使用 IO.popen 在循环中运行一系列命令行命令。然后我需要在循环外运行另一个命令。在循环中的命令不能运行,直到循环中的所有命令都终止。

I'm using IO.popen in Ruby to run a series of command line commands in a loop. I then need to run another command outside of the loop. The command outside of the loop cannot run until all of the commands in the loop have terminated.

如何让程序等待这种情况发生?

How do I make the program wait for this to happen? At the moment the final command is running too soon.

例如:

for foo in bar
    IO.popen(cmd_foo)
end
IO.popen(another_cmd)

所有 cmd_foos 需要在运行 another_cmd 之前返回。 p>

So all cmd_foos need to return before another_cmd is run.

推荐答案

我想你需要分配 IO.popen 调用循环中的变量,并继续调用 read(),直到 eof()所有。

I think you'd need to assign the results from the IO.popen calls within the cycle to the variables, and keep calling read() on them until eof() becomes true on all.

然后你知道所有的程序都已经完成执行,你可以启动 another_cmd

Then you know that all the programs have finished their execution and you can start another_cmd.

这篇关于如何等待进程完成使用IO.popen?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆