What is the correct fork () method in FastCGI?

I have an application under Catalyst + FastCGI. And I want it to fork () to do some work in the background.
I used this code for simple CGI for a long time (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 of this in FastCGI, but without success. How should I fork in 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 am sending a request with an AJAX call and have the error "502 Bad Gateway" in the firebug console.

+3
source share
2 answers

This part will not work well with FastCGI:

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

, FastCGI.

setsid() close() s . .

+2

, FAQ : http://www.fastcgi.com/docs/faq.html#Perlfork

$request->Detach(); $request->Attach(); , , $request - FCGI. , .

Catalyst:: Engine:: FastCGI Catalyst:: Engine:: FastCGI, $request, run() ( , CPAN).

+1

Source: https://habr.com/ru/post/1740144/


All Articles