Reason for the error

The parent and child processes all share the same database connection. When the first child process exits it will disconnect from the database, which means the same connection all processes are using will be disconnected, causing any further queries to fail.

The solution

The solution is to disconnect from the database before forking the sub processes and then establish a new connection in each process. The fourth parameter also should be passed to the mysql_connect function as "true" to ensure a new link is established; the default is to share an existing connection is the login details are the same.


//fork the process to work in a daemonized environment
file_put_contents($log, 'Status: starting up
', FILE_APPEND);
$pid = pcntl_fork();
if($pid == -1){
  file_put_contents($log, 'Error: could not daemonize process
', FILE_APPEND);
  return 1; //error
}
else if ($pid)
{
  return 0;
}
else{
  $connect = sql_connect($db_host, $db_user, $db_pass, $db_name);
  //the main process
  while(true){
    $result = SendEmailValidation();
    file_put_contents($log, $result, FILE_APPEND);
    sleep(1);
  }//end while
}//end if



http://www.electrictoolbox.com/mysql-connection-php-fork/

저작자 표시
신고

+ Recent posts

티스토리 툴바