您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

带有超时的分叉子进程并捕获输出

带有超时的分叉子进程并捕获输出

您可以使用IO.pipe并告诉Process.spawn使用重定向输出,而无需外部gem。

当然,仅从Ruby 1.9.2开始(我个人建议1.9.3)

以下是Spinach BDD在内部用于捕获输出和err输出的简单实现:

# stdout, stderr pipes
rout, wout = IO.pipe
rerr, werr = IO.pipe

pid = Process.spawn(command, :out => wout, :err => werr)
_, status = Process.wait2(pid)

# close write ends so we Could read them
wout.close
werr.close

@stdout = rout.readlines.join("\n")
@stderr = rerr.readlines.join("\n")

# dispose the read ends of the pipes
rout.close
rerr.close

@last_exit_status = status.exitstatus

原始来源位于features / support / filesystem.rb中

强烈建议您阅读Ruby自己的Process.spawn文档。

希望这可以帮助。

PS:我将超时实现留给您做功课;-)

其他 2022/1/1 18:18:02 有581人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶