http://www.perlmonks.org?node_id=874580

donfox1 has asked for the wisdom of the Perl Monks concerning the following question:

I'm attemptintg to run several separate forked SQL queries from a function in a program. I'd like to avoid zombies, run each query in the background and with nohup. The final result will be a report on data counts.

do_queries() will do the work by calling do_query() many times passing a new argument each time. I'll eventually pass in an array of all the arguments for each query.

The question is: How to background each query? Running just one query the program (parent) waits for the child to return, i.e. freezes until done. It should not freeze until all the queries are forked and done.

Also, it this is not the right approach what should I do?

sub do_queries { $rc = get_query('t_ub92_hdr_ext_key', 'D', '20101001', '20101031') +; print "-------Query Returned is $rc\n\n\n"; defined(my $pid = fork) or die "Cannot fork: $!"; unless($pid) { print "Child process running\n\n\n"; $rc = do_query($rc); if ($rc == 0) { print "Query Done!\n"; print $time = localtime(); } exit 0; print "This is the parent process(waiting!) and child ID is $p +id\n\n\n"; while (wait() != -1) {} do { my $kid = waitpid(-1, WNOHANG); } while $kid > 0; print "Parent Process is Exiting\n"; } }