I have just realized there’s one very important distinction I forgot to make: I don’t have to wait for the job to finish to respond to the HTTP request. Which means I’m fine with Forks::Super:
#!/usr/bin/env perl
use strict;
use warnings;
use Forks::Super MAX_PROC => 1, ON_BUSY => 'queue';
my $app = sub {
my $env = shift;
fork sub {
# lengthy operation
};
return [202, ['Content-Type'=>'text/plain', 'Content-Length'=>8], ["Accepted"]];
};
Now servicing the request finishes immediately, the long operation runs in background and there’s always at most one of them running. Forks::Super
looks complex and takes ages to install, so I’d be happy if someone knows a more lightweight module offering a similar feature.