某些命令挂起我的 Ruby Web 应用程序

发布于 2024-08-23 07:51:25 字数 671 浏览 3 评论 0原文

我正在玩 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

悟红尘 2024-08-30 07:51:25

尝试将 /dev/null 通过管道传输到您的子进程中:

IO.popen("#{params[:command]} </dev/null") do ...

Try piping /dev/null into your child process:

IO.popen("#{params[:command]} </dev/null") do ...
小女人ら 2024-08-30 07:51:25

解决方案:我只记得去年我在这里询问过有关 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文