pcntl_fork and the MySQL connection is gone

前端 未结 5 1863
执笔经年
执笔经年 2020-12-10 11:39

I have a foreach loop that forks within it. After the process forks, it accesses the database. I get an error:

SQLSTATE[HY000]: General error: 2006 MySQL ser         


        
相关标签:
5条回答
  • 2020-12-10 11:59

    Except it is not a problem. It is the way pcntl_fork was designed. Any extension (as the documentation clearly states) that maintains it's own file descriptors will then have corrupted descriptors because all children an parents share the same file descriptors.

    0 讨论(0)
  • 2020-12-10 12:03

    You can avoid closing connection when forked process exit, if you kill forked process with SIGKILL.

    <?php
    $dbh = new PDO('pgsql:host=localhost', $username, $password);
    $pid = pcntl_fork();
    if($pid == 0){
            register_shutdown_function(function(){
                    posix_kill(getmypid(), SIGKILL);
            });
            exit;
    }
    sleep(1);
    $statement = $dbh->query('select 1');
    var_dump($statement);
    

    The reason of this behavior, that when PHP process is exit, than PHP sends to database server "Terminate connection" command. But socket will be closed by system only when all links to socket is closed. Using SIGKILL help us to avoid sending "Terminate connection" command to database server.

    0 讨论(0)
  • 2020-12-10 12:06

    You need to close the MySQL connection on your parent process and then make a new connection for each child.

    <?php
    $dbh = new PDO('pgsql:host=localhost', $username, $password);
    $pid = pcntl_fork();
    if(!$pid){
            // make new connection
            $newConnection = new PDO('pgsql:host=localhost', $username, $password);
            // do something in the child process.
            exit;
    }else{ 
            // parent node
            $dbh = null; // close PDO connection
    }
    
    0 讨论(0)
  • 2020-12-10 12:10

    This helped for me: http://www.electrictoolbox.com/mysql-connection-php-fork/

    Especially mysql_connect($server, $username, $password, true);

    0 讨论(0)
  • 2020-12-10 12:20

    (comment --> answer per poster's request)

    Reading more into it I see forked children do inherit their parent's db connection, and it is a known problem: http://php.net/manual/en/function.pcntl-fork.php#70721

    0 讨论(0)
提交回复
热议问题