my solution is here: sub fork_temporary_child { my $result = fork(); unless( $result ) { # Do not use SQL in child dbh->{InactiveDestroy} = 1; disconnect_dbh; } return $result; }
On Tue, Aug 25, 2009 at 7:11 AM, Victor Danilchenko <vic...@askonline.net>wrote: > Hi all, > > I need to be able to fork an Apache process in daemon form, to do > some housekeeping which might potentially take a few seconds. However, when > I do that, I start getting SQL errors (of the "connection lost" type) in the > browser. I do the fairly standard cleanup to daemonize the child process, > but of course it needs to retain the SQL socket open. Here is my forking > code: > > sub modperl_kamikaze_fork () { > # You will have to do CORE::exit(0) at the end of the execution. > > $SIG{CHLD} = 'IGNORE'; > get_m->flush_buffer; > defined (my $kid = fork) or die "Cannot fork: $!\n"; > return $kid if $kid; > > my $r = get_r; > close STDIN; open STDIN, '/dev/null' > or die "Can't read /dev/null: $!"; > close STDOUT; open STDOUT, '>/dev/null' > or die "Can't write to /dev/null: $!"; > close STDERR; open STDERR, '>>/tmp/form.log' > or die "Cannot open /tmp/fork.log\n"; > setsid > or die "Can't start a new session: $!"; > > my $oldfh = select STDERR; > local $| = 1; > select $oldfh; > warn "Child (PID $$) spawned.\n"; > > $r->child_terminate; > } > > > The Apache2:Subprocess doesn't help me, because I need not to spawn > an external process, but to finish processing in mod_perl context -- just > without bugging the user with it. > > Any ideas on how to either fork better, or how to solve this without > forking (e.g. is there a way to 'append' a function call to the request > after the rest of the request is completed)? > > Many thanks in advance. > > -- > Victor Danilchenko > Senior Software Engineer, AskOnline.net > vic...@askonline.net - 617-273-0119 >