在脚本完成之前用Ruby CGI返回响应?

时间:2010-11-15 21:58:29

标签: ruby cgi

任何人都知道如何在 CGI脚本执行完毕之前在Ruby 中发送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

1 个答案:

答案 0 :(得分:3)

我回答了自己的问题。结果我只需要在子进程中关闭$ 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