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!

The way forward always starts with a minimal test.

Replies are listed 'Best First'.
Re^2: Dancer and forking
by Anonymous Monk on Apr 30, 2018 at 10:04 UTC
    sorry,what do you mean?

      The forked child process is meant to return the content, not to the parent process, but to the connected client. The exit causes all open file handles to be flushed and closed. Thus the content is delivered to the client.

      Without the exit, the forked child would continue to execute the parent's code after returning from the anonymous sub.

      perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
        And because the filehandles which are sshared have been opened by the parent still at large are shares with the child so when closing up with exit the stdout is delivered to the client/browser?

      Apologies, had no time earlier. Answer updated. And thanks shmem for jumping in in the meantime!


      The way forward always starts with a minimal test.
Re^2: Dancer and forking
by Anonymous Monk on Apr 30, 2018 at 11:31 UTC
    And what happens when the user reloads the browser? isn't the request fired again so we go over the same process on more time?

      That would be up to how you code your route handler :-) In the example I showed, the user's browser has been redirected to the static file, so the user would be reloading that.


      The way forward always starts with a minimal test.