php pcntl functions and postgresql

php pcntl functions and postgresql

am 29.01.2006 07:37:43 von Chris Smith

------=_Part_693_13624180.1138516663693
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

Hi all,

This is a bit of a strange one and I'm not sure where to start looking, so
any / all suggestions welcome.

I'm using php with the process control functions and want each thread to
share the main db connection.

Running php 5.1.2 and postgresql 8.1.2.

My code looks like:

#!/usr/local/php/bin/php -q
declare(ticks=3D1);
$time =3D time();

$connection_string =3D 'dbname=3Dxxxxxxxx user=3Dxxxxxxxx password=3D';
if (!$connection_result =3D pg_pconnect($connection_string)) {
echo 'Unable to connect: ' . pg_last_error() . "\n";
exit();
}

echo "Connected ok\n";

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

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

if ($pid == 0) {
$childcount++;
usleep(100);
pcntl_wait($status);
} else {
echo "\nchildcount: " . $childcount . "\n";
var_dump($connection_result);
$qry =3D "select version() AS version";
$result =3D pg_query($connection_result, $qry);
if (!$result) {
echo "error: " . pg_last_error() . "\n";
} else {
$row =3D pg_fetch_assoc($result);
echo $row['version'] . "\n";
}
exit();
}
}

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


When I run it I get:

childcount: 0
resource(1) of type (pgsql link persistent)
PostgreSQL 8.1.2 on i686-pc-linux-gnu, compiled by GCC gcc (GCC) 3.3.5(Debi=
an 1:
3.3.5-13)

childcount: 1
resource(1) of type (pgsql link persistent)
error: server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.

childcount: 2
resource(1) of type (pgsql link persistent)
error: server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.


The 'resource(1) of type (pgsql link persistent)' shows the connection is
still valid as far as php is concerned.

In the postgresql logs I get:

LOG: connection received: host=3D[local]
LOG: connection authorized: user=3Dxxxxxx database=3Dxxxxxx
LOG: statement: select version() AS version
LOG: duration: 3.516 ms
LOG: duration: 3.516 ms statement: select version() AS version
LOG: disconnection: session time: 0:00:00.02 user=3Dxxxxxx database=3Dxxxx=
xx
host=3D[local]


Turning up debugging / logs in postgresql didn't show anything extra.

So I think either:
a) I'm doing something wrong
b) It's a bug in php
c) It's a bug in the postgresql php "driver"

I don't think it's a or b because I change it all to mysql and it works, it
doesn't drop the connection like it is here.

I'm not sure where to start looking from here... so any ideas/comments
please send them through!

Thanks,
Chris.

------=_Part_693_13624180.1138516663693
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

Hi all,



This is a bit of a strange one and I'm not sure where to start looking, so =
any / all suggestions welcome.



I'm using php with the process control functions and want each thread to sh=
are the main db connection.



Running php 5.1.2 and postgresql 8.1.2.



My code looks like:



#!/usr/local/php/bin/php -q

<?php

declare(ticks=3D1);

$time =3D time();



$connection_string =3D 'dbname=3Dxxxxxxxx user=3Dxxxxxxxx password=3D';

if (!$connection_result =3D pg_pconnect($connection_string)) {

    echo 'Unable to connect: ' . pg_last_error() . "\n&=
quot;;

    exit();

}



echo "Connected ok\n";



$childcount =3D 0;

$childrencount =3D 10;

while($childcount < $childrencount) {

    $pid =3D pcntl_fork();



    if ( $pid == -1 ) {

        die("error\n");

    }



    if ($pid == 0) {

        $childcount++;

        usleep(100);

        pcntl_wait($status);

    } else {

        echo "\nchildcount: " . $ch=
ildcount . "\n";

        var_dump($connection_result);

        $qry =3D "select version() AS ve=
rsion";

        $result =3D pg_query($connection_resu=
lt, $qry);

        if (!$result) {

            echo "error: =
" . pg_last_error() . "\n";

        } else {

            $row =3D pg_fetch_=
assoc($result);

            echo $row['version=
'] . "\n";

        }

        exit();

    }

}



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

?>





When I run it I get:



childcount: 0

resource(1) of type (pgsql link persistent)

PostgreSQL 8.1.2 on i686-pc-linux-gnu, compiled by GCC gcc (GCC) 3.3.5 (Deb=
ian 1:3.3.5-13)



childcount: 1

resource(1) of type (pgsql link persistent)

error: server closed the connection unexpectedly

        This probably means the server t=
erminated abnormally

        before or while processing the r=
equest.



childcount: 2

resource(1) of type (pgsql link persistent)

error: server closed the connection unexpectedly

        This probably means the server t=
erminated abnormally

        before or while processing the r=
equest.





The 'resource(1) of type (pgsql link persistent)' shows the connection is s=
till valid as far as php is concerned.



In the postgresql logs I get:



LOG:  connection received: host=3D[local]

LOG:  connection authorized: user=3Dxxxxxx database=3Dxxxxxx

LOG:  statement: select version() AS version

LOG:  duration: 3.516 ms

LOG:  duration: 3.516 ms  statement: select version() AS version<=
br>
LOG:  disconnection: session time: 0:00:00.02 user=3Dxxxxxx database=
=3Dxxxxxx host=3D[local]





Turning up debugging / logs in postgresql didn't show anything extra.



So I think either:

a) I'm doing something wrong

b) It's a bug in php

c) It's a bug in the postgresql php "driver"



I don't think it's a or b because I change it all to mysql and it works, it=
doesn't drop the connection like it is here.



I'm not sure where to start looking from here... so any ideas/comments plea=
se send them through!



Thanks,

Chris.




------=_Part_693_13624180.1138516663693--