Re: pcntl_fork?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Chris wrote:
Hi Peter,

If you can get it working can you send me a sample?
Certainly... database connections must be CLEAN! Meaning, you should close them when they're no longer needed.

Additionally, each fork should open its own DB connection.

My completed code:
<?
require_once('../private/sql.php');
require_once('include-crawl.php');

if (phpversion() > 5) {
   date_default_timezone_set('UTC');
}

$pgconnectstring='dbname=' . returndbname() . ' user=' . returndbuser() . ' password=' . returndbpass() . ' host=redback.10mbit.biz';
$database = pg_connect($pgconnectstring);

$pquery = "SELECT count(key) AS count FROM tvguide_channels WHERE crawl=true;";
$presult = pg_query($database,$pquery);
$row = pg_fetch_object($presult,0);
pg_close($database);

$childcount = 0;
$childrencount = $row->count;
while($childcount < $childrencount) {
   $pid = pcntl_fork();

   if ( $pid == -1 ) {
       die("Unable to fork\n");
   } elseif ($pid == 0) {
       $childcount++;
       sleep(10);
       continue;
   } else {
       $db = pg_connect($pgconnectstring);
$cquery = "SELECT id FROM tvguide_channels WHERE crawl=true ORDER BY key ASC LIMIT 1 OFFSET ".$childcount.";";
       $cresult = pg_query($db, $cquery);
if ($cresult && (pg_num_rows($cresult) == 1)) {
           $row = pg_fetch_object($cresult,0);
//echo $childcount ." ". $row->id ." ". posix_getpid() ." ". posix_getppid() ."\n";
           tvxml_parsechannel($db,$row->id);
           pg_close($db);
       } else {
           echo $childcount ." ". pg_last_error($db) ."\n";
       }
exit();
   }
}

?>

Try this:
<?php
declare(ticks=1);
$time = time();

$childcount = 0;
$childrencount = 10;
while($childcount < $childrencount) {
        $pid = pcntl_fork();

        if ( $pid == -1 ) {
                die("error\n");
        }

        if ($pid == 0) {
                $childcount++;
                pcntl_wait($status);
        } else {
$connection_string = 'dbname=xxxx user=xxxx password=xxxx';
if (!$connection_result = pg_pconnect($connection_string)) {
       echo 'Unable to connect: ' . pg_last_error() . "\n";
       exit();
}

echo "Connected ok\n";

                $qry = "select version() AS version";
                $result = pg_query($connection_result, $qry);
                if (!$result) {
                        echo "error: " . pg_last_error() . "\n";
                } else {
                        $row = pg_fetch_assoc($result);
                        echo $row['version'] . "\n";
                }
pg_close($connection_result);
                exit();
        }
}

echo 'took ' . (time() - $time) . ' seconds' . "\n";
?>

IE: you were connecting to the DB too early, at a point where it wasn't needed.

On another note, my new script that forks uses approx 24mb of memory - a very nice cut down on its usage :D

Regards,
Peter Hoskin

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux