Script "hanging" for a long time before exit

Script "hanging" for a long time before exit

am 17.05.2007 12:31:19 von Sean Timmins

Apologies if this should be directed to the main php list, but I cannot
replicate the exact same problem without the use of a backend database so=
=20I
thought here might be more appropriate.

I recently upgraded a system from php 4.4.2 to php 5.2.1, and one of my
scripts has started behaving very strangely. I've tried google but come u=
p
blank so far.

Basically what the script does is select a large amount of data from a
mysql (4.1.21) database, do some manipulation, the plots a graph (using
jpgraph 2.2). I removed all the jpgraph code and am still getting the
problem. What happens is the script 'hangs' for a period of time after
executing the last php command before actually exiting. During this perio=
d
of time the process takes up a large amount of CPU time (a whole single
processor mostly) but if you truss the process you get no output until it=

finally exits (when you see stdin/out/err closed etc).

I have the code down to about the smallest I can and still show you what =
is
happening. The include.php is simply the databaae connection details. Wit=
h
the data I have the query returns 53068 rows. You call the script with th=
e
argument 0, 1 or 2 to use the 3 different queries (For 2, 3 or 4 total
columns returned). With 0 or 1 the script exits immedaitely after printin=
g
'Exiting', with 2 there is an 18-20 second delay.

The columns returned are:

date: a date/time stamp
r_per_sec: A floating point numer
b_workers: An integer
I_workers: An integer

If I comment out any one of the three lines marked "*KEY LINE*" then the
script exits immediately. I've also included below the output from the
comand line when I run the PHP script with the 0 1 and 2 arguments to
should you the 'delay' in exiting. I have tried with and without the Zend=

Optimizer and definately do not get the same hang with php v4.x

*** CODE ***
function my_log($message)
{
=A0 $date =3D date("H:i:s");
=A0 print ("$date: $message\n");
}

my_log("Script Starting");
include "include.php";

# Connect to and select database
$db =3D mysql_connect("$hostname", "$username", "$password");
mysql_select_db($database);

# Metrics array to replace all the code which worked out the metrics
# query variable to replace all the code which worked out the SQL
if ($argv[1] == "0")
{
=A0 $metrics[0] =3D array ("apollo_APACHE_external.r_per_sec");
=A0 $query =3D "SELECT DATE_FORMAT(apollo_APACHE_external.date,\"%d/%m/%y=

%H:%i\") AS mydate,apollo_APACHE_external.r_per_sec AS c0 FROM
apollo_APACHE_external WHERE =A0apollo_APACHE_external.date >=3D 20040101=
000000
AND apollo_APACHE_external.date <=3D 20040930230000 ORDER BY
apollo_APACHE_external.date";
}
elseif ($argv[1] == "1")
{
=A0 $metrics =3D array ("apollo_APACHE_external.r_per_sec",
"apollo_APACHE_external.b_workers");
=A0 $query =3D "SELECT DATE_FORMAT(apollo_APACHE_external.date,\"%d/%m/%y=

%H:%i\") AS mydate,apollo_APACHE_external.r_per_sec AS
c0,apollo_APACHE_external.b_workers AS c1 FROM apollo_APACHE_external
WHERE =A0apollo_APACHE_external.date >=3D 20040101000000 AND
apollo_APACHE_external.date <=3D 20040930230000 ORDER BY
apollo_APACHE_external.date";
}
elseif ($argv[1] == "2")
{
=A0 $metrics =3D array ("apollo_APACHE_external.r_per_sec",
"apollo_APACHE_external.b_workers", "apollo_APACHE_external.i_workers");
=A0 $query =3D "SELECT DATE_FORMAT(apollo_APACHE_external.date,\"%d/%m/%y=

%H:%i\") AS mydate,apollo_APACHE_external.r_per_sec AS
c0,apollo_APACHE_external.b_workers AS c1,apollo_APACHE_external.i_worker=
s
AS c2 FROM apollo_APACHE_external WHERE apollo_APACHE_external.date >=3D
20040101000000 AND apollo_APACHE_external.date <=3D 20040930230000 ORDER =
BY
apollo_APACHE_external.date";
}
else
{
=A0 exit;
}

my_log("executing Query");
$result =3D mysql_query($query);
my_log ("Number of results:" . mysql_num_rows($result));

$count=3D0;
my_log("Fetching Results");
while ($row =3D mysql_fetch_array($result))
{
=A0 # Build the x axis data
=A0 $datax[$count] =3D $row[mydate]; =A0 # *KEY LINE*

=A0 # Build the y axis data
=A0 for ($i=3D0; $i < count($metrics); $i++)
=A0 {
=A0 =A0 $datay[$i][$count] =3D $row["c$i"]; =A0 # *KEY LINE*
=A0 }
=A0 $count++;
}
my_log("Freeing result set");
mysql_free_result($result);
my_log("Closing DB connection");
mysql_close($db);
my_log("Done");

# First do the X axis points
$newdatax =3D array();

# Reassign newdatax back to datax
$datax =3D $newdatax; =A0 # *KEY LINE*
my_log("Exiting");
?>
*** CODE***

*** COMAND LINE OUTPUT ***
$ php -v
PHP 5.2.1 (cli) (built: May =A03 2007 11:15:31)
Copyright (c) 1997-2007 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies
=A0 =A0 with Zend Extension Manager v1.2.0, Copyright (c) 2003-2007, by Z=
end
Technologies
=A0 =A0 with Zend Optimizer v3.2.8, Copyright (c) 1998-2007, by Zend
Technologies
$ php plot_graph.php 2; date
16:27:59: Script Starting
16:27:59: executing Query
16:28:01: Number of results:53068
16:28:01: Fetching Results
16:28:03: Freeing result set
16:28:03: Closing DB connection
16:28:03: Done
16:28:03: Exiting
Thu May 10 16:28:22 BST 2007

$ php plot_graph.php 1; date
16:28:37: Script Starting
16:28:37: executing Query
16:28:39: Number of results:53068
16:28:39: Fetching Results
16:28:41: Freeing result set
16:28:41: Closing DB connection
16:28:41: Done
16:28:41: Exiting
Thu May 10 16:28:41 BST 2007

$ php plot_graph.php 0; date
16:28:45: Script Starting
16:28:45: executing Query
16:28:47: Number of results:53068
16:28:47: Fetching Results
16:28:48: Freeing result set
16:28:48: Closing DB connection
16:28:48: Done
16:28:48: Exiting
Thu May 10 16:28:48 BST 2007
$
*** COMMAND LINE OUTPUT ***


---
Sean Timmins
Systems Administrator
Web Publishing Systems
John Wiley & Sons Ltd.
The Atrium
Southern Gate
Chichester
West Sussex
PO19 8SQ

Tel: +44 (0) 1243 770395
Fax +44 (0) 1243 770379
ob: +44 (0) 7703 203546

Wiley Bicentennial:
1807-2007 Knowledge for Generations



------------------------------------------------------------ ----------
The information contained in this e-mail and any subsequent
correspondence is private and confidential and intended solely=20
for the named recipient(s). If you are not a named recipient,=20
you must not copy, distribute, or disseminate the information,=20
open any attachment, or take any action in reliance on it. If you=20
have received the e-mail in error, please notify the sender and delete
the e-mail. =20
=20
Any views or opinions expressed in this e-mail are those of the=20
individual sender, unless otherwise stated. Although this e-mail has=20
been scanned for viruses you should rely on your own virus check, as=20
the sender accepts no liability for any damage arising out of any bug=20
or virus infection.

John Wiley & Sons Limited is a private limited company registered in
England with registered number 641132.

Registered office address: The Atrium, Southern Gate, Chichester,
West Sussex, PO19 8SQ.
------------------------------------------------------------ ----------

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

Re: Script "hanging" for a long time before exit

am 18.05.2007 02:26:17 von dmagick

> $ php plot_graph.php 2; date
> 16:27:59: Script Starting
> 16:27:59: executing Query
> 16:28:01: Number of results:53068
> 16:28:01: Fetching Results
> 16:28:03: Freeing result set
> 16:28:03: Closing DB connection
> 16:28:03: Done
> 16:28:03: Exiting
> Thu May 10 16:28:22 BST 2007

Looks like this is the only one you need to worry about.

Does it happen consistently (ie every time) ? Maybe the server was under
load and it took a while to release the resources or something.

If it does happen consistently maybe you can use gdb or something to
work out what/where/why.

--
Postgresql & php tutorials
http://www.designmagick.com/

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