SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    mysql with pcntl.... how?

    I received the error message "MySQL server has gone away" with PCNTL/MySQL. It looks like MySQL has to be treated differently with PCNTL, but I don't know exactly how to do it yet. Anyone have experience with this?

    My code is something like this:
    PHP Code:
    $S mysql_query("SELECT * FROM Units LIMIT 6") or die(mysql_error());
    while(
    $S2 mysql_fetch_assoc($S)) {
        
    $Units[] = $S2['ID'];
    }


    foreach(
    $Units as $ID) {
        
    $pids[$ID] = pcntl_fork();
        if(!
    $pids[$ID]) {
            
    $Query2 mysql_query("SELECT * FROM UnitData WHERE UnitID = '$ID'") or die();
            while(
    $Return2 mysql_fetch_assoc($Query2)) {
                
    // DO STUFF
            
    }
            
    file_get_contents('something');
            exit();
        }
    }
            
    foreach(
    $Units as $ID) {
      
    pcntl_waitpid($pids[$ID], $statusWUNTRACED);


  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Can I ask...why are you using the old MySQL extension?
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Umm, cause lol

  4. #4
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,182
    Mentioned
    66 Post(s)
    Tagged
    2 Thread(s)
    mysql_* is not thread safe. Use pdo. Have each process open up its own connection as well

  5. #5
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I found this post:

    The reason for the MySQL "Lost Connection during query" issue when forking is the fact that the child process inherits the parent's database connection. When the child exits, the connection is closed. If the parent is performing a query at this very moment, it is doing it on an already closed connection, hence the error.

    An easy way to avoid this is to create a new database connection in parent immediately after forking. Don't forget to force a new connection by passing true in the 4th argument of mysql_connect():

    <?php
    // Create the MySQL connection
    $db = mysql_connect($server, $username, $password);

    $pid = pcntl_fork();

    if ( $pid == -1 ) {
    // Fork failed
    exit(1);
    } else if ( $pid ) {
    // We are the parent
    // Can no longer use $db because it will be closed by the child
    // Instead, make a new MySQL connection for ourselves to work with
    $db = mysql_connect($server, $username, $password, true);
    } else {
    // We are the child
    // Do something with the inherited connection here
    // It will get closed upon exit
    exit(0);
    ?>

    This way, the child will inherit the old connection, will work on it and will close upon exit. The parent won't care, because it will open a new connection for itself immediately after forking.

    Hope this helps.

  6. #6
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    72 Post(s)
    Tagged
    0 Thread(s)
    the more apt approach would be to have the child invoke it's own connection - if you ever have to fork more than once, the strategy outlined above will still run the problem of all children using a single connection. There will only be one parent. There can be many children.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  7. #7
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Honestly, I don't know enough to *understand* how it works, but I got my script going and oh my god it's faster than I could have imagined. I'm shocked to be honest lol.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •