in reply to Dancer and forking
(Because of the exit.)
Edit: sorry for the brief and cryptic answer, I have more time now, let me try to explain.
When you fork a child process from within the Dancer preforked process, that process will not return anything to the browser unless you use an explicit return. Correspondingly, since you have forked a child to handle the actual work, the parent process will not exit and will persist as a zombie unless you provide an explicit exit statement.
Because you are doing your slow processing off-line, you'll have to return something in the mean time to the browser. It's possible to use Ajax, websockets and even Server-Sent Events, but the simplest way might be to return a temporary page with a note to check back soon (or possibly send an email message when the work is done). In the meantime, off line, you will rewrite the file.
Here's an example of how I've done it:
package My::RouteHandler; use Dancer; use Path::Tiny; use Data::GUID; use Time::Piece; ... post '/some/route' => sub { my %args = params; if ( $args{'long_task'} ) { my $guid = Data::GUID->new->as_string; my $url = '/files/results/' . $guid . '.html'; my $path = config->app_root . '/app/restricted' . $url; my $file = path( $path )->touch; fork and do { $file->spew( sprintf "Results not ready. Please reload thi +s page in a few minutes.<BR>URL: %s<BR>Created: %s", $url, gmtime->da +tetime ); return redirect $url; }; # now we are off line my $stuff = do_some_lengthy_task(); $file->spew( $stuff ); exit; # because we already forked and returned to the browser } else { ... } }; 1; __END__
Hope this helps!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Dancer and forking
by Anonymous Monk on Apr 30, 2018 at 10:04 UTC | |
by shmem (Chancellor) on Apr 30, 2018 at 10:24 UTC | |
by Anonymous Monk on Apr 30, 2018 at 11:42 UTC | |
by 1nickt (Canon) on Apr 30, 2018 at 10:41 UTC | |
|
Re^2: Dancer and forking
by Anonymous Monk on Apr 30, 2018 at 11:31 UTC | |
by 1nickt (Canon) on Apr 30, 2018 at 11:45 UTC |