[dancer-users] Dancer and forking
Johandry Amador Segui
johandry at gmail.com
Wed Jul 8 00:58:06 BST 2015
Hi Andy,
I tried to use threads and it did not worked for me (maybe because of non safe thread modules I am using) so I use fork in a production application and it works good. I have some issues some times with MySQL that I have not fixed yet but they do not happen very often.
This is an example of how I use it:
sub execute {
# These 2 lines below were to eliminate the error that I mentioned before. It did not eliminated them just reduce them
my $dbh = database();
$dbh->{InactiveDestroy} = 1;
my $pid = fork();
if ($pid) {
# This is the parent (Dancer) in case you want to do something here
debug “Process started with PID $pid\n”;
} elsif ($pid == 0) {
# This is the child, here is where I do all I need
$template = ...
} else {
debug “Could not fork: $!\n”;
}
# I have a variable to know if I’m running this sub from the web or from CLI
if ($cli) {
my $output = waitpid($pid, 0);
print “The process finish with output: $output”;
}
return $template;
}
This is just an example, not the entire code. If someone has a suggestion or improvement, it is welcome.
Thanks,
Johandry
> On Jul 7, 2015, at 2:58 PM, Andrew Beverley <andy at andybev.com> wrote:
>
> Dear all,
>
> Is there anything that I should be aware of if using fork() in a dancer
> application?
>
> I have some cache update code which can take a while to run, so I'd rather do it
> in the background. I've therefore done something like this:
>
> post '/data' => sub {
> my $template = ...;
> fork and return $template;
> update_cache;
> exit;
> }
>
> The actual code is in a module, but you get the idea.
>
> Thanks,
>
> Andy
>
> _______________________________________________
> dancer-users mailing list
> dancer-users at dancer.pm
> http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
More information about the dancer-users
mailing list