use strict; use warnings; use File::Temp (); use POSIX ":sys_wait_h"; my $app = sub { my $temp = File::Temp->new(UNLINK => 0); my $filename = $temp->filename; my $pid = fork(); defined $pid or return [ 500, ["Content-type" => 'text/plain'], ["Could not fork: $!"] ]; if ($pid) { # parent process: reap children which are done, # then print response and return my $kid; do { $kid = waitpid(-1, WNOHANG); } while $kid < -1; return [ 200, ["Content-type" => 'text/plain'], ["Started process with pid '$pid'\n", "Printing to '$filename'\n", ], ]; } else { # child process: take 10 seconds to print, then exit my $count = 1; while ($count < 10) { print $temp $count++,"\n"; sleep 1; } close $temp; exit 0; } }