某些命令挂起我的 Ruby Web 应用程序
我正在玩 Rails & Sinatra,我想在服务器上执行命令。这些命令是从表单输入的。问题是,如果我输入一个需要输入的命令,我的整个应用程序就会挂起。这是我用来执行它们的代码:
@threads << Thread.new do
Thread.current["buffer"] = ""
puts "starting #{params[:command]}"
IO.popen(params[:command]) do |io|
io.each_line {|l| Thread.current["buffer"] += l}
end
end
这对于像 ls
这样的简单命令来说没问题……但是例如,如果我输入 pause
,它会期望用户按下一键继续,一切都挂了。我该如何解决这个问题?
编辑:我只记得去年我在这里询问过有关 Ruby 线程行为的问题: 为什么这是像没有线程一样运行的吗? 。我尝试使用 1.9.1 解释器运行 Sinatra,它成功了。然而在 1.8.6 下却没有。如果mod愿意,他可以关闭这个问题。
I'm playing around with Rails & Sinatra, and I want to execute commands on the server. Those commands are entered from a form. The thing is, if I enter a command which expects input, my whole app hangs. Here's the code I'm using to execute them:
@threads << Thread.new do
Thread.current["buffer"] = ""
puts "starting #{params[:command]}"
IO.popen(params[:command]) do |io|
io.each_line {|l| Thread.current["buffer"] += l}
end
end
this works ok for simple commands like ls
... but for example if I enter pause
which will expect the user to press a key to continue, everything hangs. How can I get around that?
EDIT: I just remembered I asked last year about Ruby thread behaviour here: Why is this running like it isn't threaded? . I tried running Sinatra using a 1.9.1 interpreter and it worked. Under 1.8.6 it doesn't however. A mod can close this question if he wants.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试将 /dev/null 通过管道传输到您的子进程中:
Try piping /dev/null into your child process:
解决方案:我只记得去年我在这里询问过有关 Ruby 线程行为的问题: 为什么这是像没有线程一样运行的吗? 。我尝试使用 1.9.1 解释器运行 Sinatra,它成功了。然而在 1.8.6 下却没有。
Solution: I just remembered I asked last year about Ruby thread behaviour here: Why is this running like it isn't threaded? . I tried running Sinatra using a 1.9.1 interpreter and it worked. Under 1.8.6 it doesn't however.