在FastCGI中fork()的正确方法是什么? [英] What's the proper way to fork() in FastCGI?
问题描述
我有一个在Catalyst + FastCGI下运行的应用程序.我希望它 fork()在后台进行一些工作.
我很早以前就将此代码用于普通CGI(并且有效):
I have an app running under Catalyst+FastCGI. And I want it to fork() to do some work in background.
I used this code for plain CGI long ago (and it worked):
defined(my $pid = fork) or die "Can't fork: $!";
if ($pid) {
# produce some response
exit 0;
}
die "Can't start a new session: $!" if setsid == -1;
close STDIN or die $!;
close STDOUT or die $!;
close STDERR or die $!;
# do some work in background
我在FastCGI下尝试了一些变体,但没有成功.在FastCGI下应该如何进行分叉?
I tried some variations on this under FastCGI but with no success. How should forking be done under FastCGI?
更新:这就是我现在拥有的:
Update: This is what I have now:
defined(my $pid = fork) or die "Can't fork: $!";
if ($pid) {
$c->stash->{message} = 'ok';
$c->detach($c->view('JSON'));
}
die "Can't start a new session: $!" if setsid == -1;
close STDIN or die $!;
close STDOUT or die $!;
close STDERR or die $!;
# do some work, then exit()
我通过AJAX调用发送了请求,并且在Firebug控制台中出现"502 Bad Gateway"错误.
I send the request with AJAX call, and have the "502 Bad Gateway" error in the firebug console.
推荐答案
我认为此常见问题解答具有正确的答案: http://www.fastcgi.com/docs/faq.html#Perlfork
I think this FAQ has the right answer: http://www.fastcgi.com/docs/faq.html#Perlfork
您应该在派生代码之前执行$request->Detach();
,在分支代码完成之后执行$request->Attach();
,其中$ request是当前的FCGI对象.至少对我有用.
You should do $request->Detach();
before the fork, and $request->Attach();
after the forking piece of code is done, where $request is the current FCGI object. At least, it worked for me.
对于Catalyst :: Engine :: FastCGI,您可能需要修补Catalyst :: Engine :: FastCGI才能访问$request
变量,因为该变量对于该位置的run()方法是本地的(在CPAN当前使用的版本).
In case of Catalyst::Engine::FastCGI you may need to patch the Catalyst::Engine::FastCGI to get access to the $request
variable, since it is local to the run() method there (in the version that is currently on CPAN).
这篇关于在FastCGI中fork()的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!