Etc

pcntl_fork php Lost connection to MySQL

데브렉스 2015. 2. 9. 05:24
반응형

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/

반응형

'Etc' 카테고리의 다른 글

Algorithm - Sort  (0) 2018.07.17
Check current running processes  (0) 2015.02.09
vi 편집기  (0) 2015.02.09
실리콘 밸리의 창업  (0) 2014.09.10
불쌍한 남자들  (0) 2014.08.04