简体   繁体   中英

Forking to Run Code in a Child Process With Perl's Dancer

I have a Dancer app in perl that handles get/post requests.

One of my example routes is below:

post '/handle_data' => sub {

    # perform some calculations
    ...
    # store some data
    ...
    # do some long running tasks
    ...
};

My question: is there an easy way to do the long running tasks in another process without blocking the request?

I've tried fork and return in the parent process, with an exit after the long running tasks for the child, but that all seems to block the response from being sent until the child completes.

Any help is appreciated.

EDIT:

I ended up switching to a full-fledged job server, specifically the perl version of Gearman

the fork and return appears to work for me.

I tried:

#!/usr/bin/perl

use Dancer;

get '/' => sub {
    fork and return "Content!\n";

    sleep 10; # do your actual work here
    warn "Child process done\n";
    exit;
};

dance;

Running that via the standalone server, or via plackup/Starman, works as expected - the "Content!\\n" is returned to the user's browser immediately, and the request is over; the forked process hangs around for ten seconds sleeping, then issues the warning (visible on the terminal) then exits.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM