在脚本完成之前用Ruby CGI返回响应? [英] Returning a response with Ruby CGI before script is finished?

查看:66
本文介绍了在脚本完成之前用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

这篇关于在脚本完成之前用Ruby CGI返回响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆