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

问题描述:

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

Anyone know how to send a CGI response in Ruby before the CGI script is finished executing?

我正在创建一个火并忘记HTTP API。我希望客户端通过HTTP将数据推送给我,并成功返回响应,然后然后刷新数据并进行一些处理(无需客户端等待响应)。

I'm creating a fire-and-forget HTTP API. I want a client to push data to me via HTTP and have the response return successfully, and then it swizzles the data and does some processing (without the client having to wait for a response).

我尝试了一些不起作用的事情,包括fork。通过HTTP调用时,以下内容仅等待5秒钟。

I've tried several things that don't work, including fork. The following will just wait 5 seconds when invoked via HTTP.

#!/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


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

I answered my own question. Turns out I just need to close $stdin, $stdout, and $stderr in the child process:

#!/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