(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.

In reply to Re: Dancer and forking by 1nickt
in thread Dancer and forking by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.