> > Yeah, something like that. The connection is definitely closed when the
> > child exits.
> >
> I can confirm this. you definitely need to open a connection for each child
> process.
> if you require a db connection in the parent process, you should close
> it before forking (and then reopen it afterwards if you still need it).

I thought that too, but this code seems to work, which seems to imply that
the child doesn't kill the existing db connection.

$conn = mysql_connect($sharedAppsDbHost, $sharedAppsDbUser,
                                 $sharedAppsDbPass, true);

foreach ($things as $thing) {

function temp($thing) {
  global $conn;
  extract(getInfo($thing)); // this function call uses a shared db
  mysqlSelectDb($dbName, $conn); // dbName is a variable gotten from the
above call
  $result = mysql_query("SELECT COUNT(*) FROM Users",
  $row = mysql_fetch_array($result, MYSQL_BOTH);
  echo "$row[0]\n";
  $pid = pcntl_fork();
  if ($pid == -1) {
    die("could not fork");
  } else if ($pid) {
    // parent, return the child pid
    echo "child pid $pid waiting\n";
    pcntl_waitpid($pid, $status, WNOHANG);
    if (pcntl_wifexited($status)) {
      echo "finished [$status] waiting\n";
  } else {
    echo "child sleeping\n";
    echo "child done\n";

My main problem here is that I have a set of helper functions (getInfo is
one of them) that uses a global db connection that exists in that helper
script.  Otherwise, I have to rewrite the function to create a new
connection every time, which I'd like not to.


Reply via email to