<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Exactly, I have it. I highlighted in <font color="#ff4013" class="">red</font> the exit from the child. <div class=""><br class=""><div class="">Also (maybe is useful) I have a print with an ‘if $cli’ to print messages in the console (debugging or logging) when the sub is executed from the CLI.<div class=""><br class=""><div class=""><div class=""><font color="#ff4013" class="">use POSIX qw(_exit);</font><br class=""><div class=""><br class=""></div><div class="">sub execute {<br class=""><br class=""><span class="Apple-tab-span" style="white-space: pre;"> </span># These 2 lines below were to eliminate the error that I mentioned before. It did not eliminated them just reduce them<br class=""><span class="Apple-tab-span" style="white-space: pre;"> </span>my $dbh = database();<br class=""><span class="Apple-tab-span" style="white-space: pre;"> </span>$dbh->{InactiveDestroy} = 1;<br class=""><br class=""><span class="Apple-tab-span" style="white-space: pre;"> </span>my $pid = fork();<br class=""><br class=""><span class="Apple-tab-span" style="white-space: pre;"> </span>if ($pid) {<br class=""><span class="Apple-tab-span" style="white-space: pre;"> </span><span class="Apple-tab-span" style="white-space: pre;"> </span># This is the parent (Dancer) in case you want to do something here<br class=""><span class="Apple-tab-span" style="white-space: pre;"> </span><span class="Apple-tab-span" style="white-space: pre;"> </span>debug “Process started with PID $pid\n”;<br class=""><br class=""><span class="Apple-tab-span" style="white-space: pre;"> </span>} elsif ($pid == 0) {<br class=""><span class="Apple-tab-span" style="white-space: pre;"> </span><span class="Apple-tab-span" style="white-space: pre;"> </span># This is the child, here is where I do all I need<br class=""><span class="Apple-tab-span" style="white-space: pre;"> </span><span class="Apple-tab-span" style="white-space: pre;"> </span>$template = …</div><div class=""><font color="#ff4013" class=""><span class="Apple-tab-span" style="white-space:pre"> </span>$exit_code= …</font></div><div class=""><font color="#ff4013" class=""><br class=""></font></div><div class=""><font color="#ff4013" class=""><span class="Apple-tab-span" style="white-space:pre"> </span>print “Something in the console” if $cli</font></div><div class=""><font color="#ff4013" class=""><span class="Apple-tab-span" style="white-space:pre"> </span>_exit($exit_code);</font><br class=""><span class="Apple-tab-span" style="white-space: pre;"> </span>} else {<br class=""><span class="Apple-tab-span" style="white-space: pre;"> </span><span class="Apple-tab-span" style="white-space: pre;"> </span>debug “Could not fork: $!\n”;<br class=""><span class="Apple-tab-span" style="white-space: pre;"> </span>}<br class=""><br class=""><span class="Apple-tab-span" style="white-space: pre;"> </span># I have a variable to know if I’m running this sub from the web or from CLI<br class=""><span class="Apple-tab-span" style="white-space: pre;"> </span>if ($cli) {<br class=""><span class="Apple-tab-span" style="white-space: pre;"> </span><span class="Apple-tab-span" style="white-space: pre;"> </span>my $output = waitpid($pid, 0);<br class=""><span class="Apple-tab-span" style="white-space: pre;"> </span><span class="Apple-tab-span" style="white-space: pre;"> </span>print “The process finish with output: $output”;<br class=""><span class="Apple-tab-span" style="white-space: pre;"> </span>}<br class=""><br class=""><span class="Apple-tab-span" style="white-space: pre;"> </span>return $template;<br class="">}</div><div class=""><br class=""></div><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Jul 7, 2015, at 11:15 PM, Andrew Beverley <<a href="mailto:andy@andybev.com" class="">andy@andybev.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class="">On 2015-07-08 00:58, Johandry Amador Segui wrote:<br class=""><blockquote type="cite" class="">Hi Andy,<br class="">I tried to use threads and it did not worked for me (maybe because of<br class="">non safe thread modules I am using) so I use fork in a production<br class="">application and it works good. I have some issues some times with<br class="">MySQL that I have not fixed yet but they do not happen very often.<br class="">This is an example of how I use it:<br class="">sub execute {<br class=""><span class="Apple-tab-span" style="white-space:pre"> </span># These 2 lines below were to eliminate the error that I mentioned<br class="">before. It did not eliminated them just reduce them<br class=""><span class="Apple-tab-span" style="white-space:pre"> </span>my $dbh = database();<br class=""><span class="Apple-tab-span" style="white-space:pre"> </span>$dbh->{InactiveDestroy} = 1;<br class=""><span class="Apple-tab-span" style="white-space:pre"> </span>my $pid = fork();<br class=""><span class="Apple-tab-span" style="white-space:pre"> </span>if ($pid) {<br class=""><span class="Apple-tab-span" style="white-space:pre"> </span><span class="Apple-tab-span" style="white-space:pre"> </span># This is the parent (Dancer) in case you want to do something here<br class=""><span class="Apple-tab-span" style="white-space:pre"> </span><span class="Apple-tab-span" style="white-space:pre"> </span>debug “Process started with PID $pid\n”;<br class=""><span class="Apple-tab-span" style="white-space:pre"> </span>} elsif ($pid == 0) {<br class=""><span class="Apple-tab-span" style="white-space:pre"> </span><span class="Apple-tab-span" style="white-space:pre"> </span># This is the child, here is where I do all I need<br class=""><span class="Apple-tab-span" style="white-space:pre"> </span><span class="Apple-tab-span" style="white-space:pre"> </span>$template = ...<br class=""><span class="Apple-tab-span" style="white-space:pre"> </span>} else {<br class=""><span class="Apple-tab-span" style="white-space:pre"> </span><span class="Apple-tab-span" style="white-space:pre"> </span>debug “Could not fork: $!\n”;<br class=""><span class="Apple-tab-span" style="white-space:pre"> </span>}<br class=""><span class="Apple-tab-span" style="white-space:pre"> </span># I have a variable to know if I’m running this sub from the web or from CLI<br class=""><span class="Apple-tab-span" style="white-space:pre"> </span>if ($cli) {<br class=""><span class="Apple-tab-span" style="white-space:pre"> </span><span class="Apple-tab-span" style="white-space:pre"> </span>my $output = waitpid($pid, 0);<br class=""><span class="Apple-tab-span" style="white-space:pre"> </span><span class="Apple-tab-span" style="white-space:pre"> </span>print “The process finish with output: $output”;<br class=""><span class="Apple-tab-span" style="white-space:pre"> </span>}<br class=""><span class="Apple-tab-span" style="white-space:pre"> </span>return $template;<br class="">}<br class="">This is just an example, not the entire code. If someone has a<br class="">suggestion or improvement, it is welcome.<br class=""></blockquote><br class="">Surely you need an exit() in there somewhere for the child process, otherwise you'll end up with lots of web server threads?<br class=""><br class="">Andy<br class=""><br class="">_______________________________________________<br class="">dancer-users mailing list<br class=""><a href="mailto:dancer-users@dancer.pm" class="">dancer-users@dancer.pm</a><br class="">http://lists.preshweb.co.uk/mailman/listinfo/dancer-users<br class=""></div></blockquote></div><br class=""></div></div></div></div></div></div></body></html>