1

的以下两种用法之间有什么区别popen3吗?

html = ''
stdin, stdout, stderr = Open3.popen3("curl #{url}")
html << stdout.read

html = ''
Open3.popen3("curl #{url}") do |stdin, stdout, stderr, wait_thr|
  result << stdout.read
end

我想知道第二种语法是否会导致某些线程阻塞。我对异步代码相当陌生,因此非常感谢任何见解!

4

2 回答 2

1

在第一种形式中,您应该明确关闭stdin和。stdoutstderr

于 2012-07-15T19:15:17.960 回答
1

您遇到阻塞行为的原因是因为您没有关闭您打开的程序(curl)的标准输入popen3- 所以 curl 仍在等待您的输入。

完成向程序发送数据后,您应该通过显式关闭标准输入stdin.close,否则它将继续期待标准输入上的输入,并popen3会挂起。

 stdin.close    # always close your stdin after you are done sending commands/data
                # or popen3 will appear to hang
于 2013-05-15T15:20:30.127 回答