Question

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

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top