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我想知道第二个语法是否会导致一些线程阻塞。我对异步代码相当陌生,所以非常感谢您的见解!
发布于 2012-07-16 03:15:18
在第一种形式中,您应该显式地关闭stdin、stdout和stderr。
发布于 2013-05-15 23:20:30
您遇到阻塞行为的原因是因为您没有关闭通过popen3打开的程序( curl )的标准输入--因此curl仍在等待您的输入。
在完成向程序发送数据之后,您应该通过stdin.close显式关闭标准输入,否则它将继续等待标准输入,并且popen3将挂起。
stdin.close # always close your stdin after you are done sending commands/data
# or popen3 will appear to hanghttps://stackoverflow.com/questions/11494688
复制相似问题