What's the proper way to fork() in FastCGI?

半世苍凉 提交于 2020-01-04 02:48:10

问题


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

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() 

I send the request with AJAX call, and have the "502 Bad Gateway" error in the firebug console.


回答1:


I think this FAQ has the right answer: http://www.fastcgi.com/docs/faq.html#Perlfork

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.

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).




回答2:


This part isn't going to work well with FastCGI:

if ($pid) {
    # print response         
    exit 0;
}

You would exit in the parent process, thus it will stop responding to FastCGI requests.

The setsid()s and close()s are to daemonize your background process. This may or may not be necessary in your case.



来源:https://stackoverflow.com/questions/2592157/whats-the-proper-way-to-fork-in-fastcgi

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!