[dancer-users] Dancer and forking

Johandry Amador Segui johandry at gmail.com
Wed Jul 8 21:59:30 BST 2015


Exactly, I have it. I highlighted in red the exit from the child. 

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.

use POSIX qw(_exit);

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 = …
		$exit_code= …

		print “Something in the console” if $cli
		_exit($exit_code);
	} 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;
}


> On Jul 7, 2015, at 11:15 PM, Andrew Beverley <andy at andybev.com> wrote:
> 
> On 2015-07-08 00:58, Johandry Amador Segui wrote:
>> 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.
> 
> Surely you need an exit() in there somewhere for the child process, otherwise you'll end up with lots of web server threads?
> 
> Andy
> 
> _______________________________________________
> dancer-users mailing list
> dancer-users at dancer.pm
> http://lists.preshweb.co.uk/mailman/listinfo/dancer-users

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.preshweb.co.uk/pipermail/dancer-users/attachments/20150708/07bb7599/attachment.html>


More information about the dancer-users mailing list