>
> >> > While this works, it unfortunately leaves behind a zombie process
> >> > every single time.
> >>
> >> You need to call pcntl_wait() or pcntl_waitpid().
> >>
> >
> > Right, but if I do that, then the parent has to wait until the child
> > completes before it exits.
>
> No it doesn't - just call pcntl_wait() with WNOHANG to check the status
> of a child.
>

Hm, so now I'm not so sure what's happening.  Before trying your suggestion,
I ran my script again without pcntl_wait so that I'd have a baseline for how
many zombie processes are being created, and it turns out that I don't think
any are.  Previously, I was doing

ps aux | awk '{ print $8 " " $2 }' | grep -w Z

to find zombie processes, and it turns out that they existed from before, as
the count didn't change after I ran my script.  That leads to another
problem, though, in that the resource for the database doesn't seem to be
available for the parent after the child exits.  Here's pseudo code for what
I'm trying to do:

foreach ($things as $thing) {
info = getInfo($thing); // uses a db connection
makeApiCall(info);
}

makeApiCall(info) {
  $pid = pcntl_fork();
  if ($pid == -1) {
    die("could not fork");
  } else if ($pid) {
    // parent, return the child pid
    echo "child pid $pid\n";
    return;
  } else {
    // do some api calls
   exit;
  }
}

But after I spawn off the process, getInfo($thing) errors out sometime later
on with an "invalid query" error, because I think the db connection is
gone.  I thought adding "exit" in the child process would be enough, but
that doesn't seem to work, I still get the same error.  Why would the child
process affect the query in the parent process, especially if I exit in the
child process?

Following your suggestion, I also tried pcntl_wait in the upperlevel
function, but still had the same problem.

Thanks for any insight,
Waynn

Reply via email to