[dancer-users] Dancer and forking

Andrew Beverley andy at andybev.com
Wed Jul 8 07:15:29 BST 2015


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



More information about the dancer-users mailing list