【问题标题】:Returning a response with Ruby CGI before script is finished?在脚本完成之前使用 Ruby CGI 返回响应?
【发布时间】:2011-05-10 12:11:21
【问题描述】:

有人知道如何在 Ruby 中发送 CGI 响应 CGI 脚本完成执行之前?

我正在创建一个即发即弃的 HTTP API。我希望客户端通过 HTTP 向我推送数据并成功返回响应,然后然后它会混合数据并进行一些处理(无需客户端等待响应)。

我尝试了几种不起作用的方法,包括 fork。当通过 HTTP 调用时,以下将只等待 5 秒。

#!/usr/bin/ruby

require 'cgi'

cgi = CGI.new
cgi.out "text/plain" do
  "1"
end

pid = fork
if pid
  # parent
  Process.detach pid
else
  # child
  sleep 5 
end

【问题讨论】:

    标签: ruby cgi


    【解决方案1】:

    我回答了我自己的问题。原来我只需要在子进程中关闭 $stdin、$stdout 和 $stderr:

    #!/usr/bin/ruby
    
    require 'cgi'
    
    cgi = CGI.new
    cgi.out "text/plain" do
      "1"
    end
    
    pid = fork
    if pid
      # parent
      Process.detach pid
    else
      # child
      $stdin.close
      $stdout.close
      $stderr.close
      sleep 5 
    end
    

    【讨论】:

    • 我可以通过Process.daemon pid得到我想要的。
    猜你喜欢
    • 1970-01-01
    • 2012-05-12
    • 2012-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-23
    • 2011-03-14
    • 2019-02-24
    相关资源
    最近更新 更多