[PHP-DB] Script 'hanging' for a long time before exit

2007-05-17 Thread 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 I
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 up
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 period
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. With
the data I have the query returns 53068 rows. You call the script with the
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 printing
'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 ***
?php
function my_log($message)
{
  $date = date(H:i:s);
  print ($date: $message\n);
}

my_log(Script Starting);
include include.php;

# Connect to and select database
$db = 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)
{
  $metrics[0] = array (apollo_APACHE_external.r_per_sec);
  $query = 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  apollo_APACHE_external.date = 2004010100
AND apollo_APACHE_external.date = 2004093023 ORDER BY
apollo_APACHE_external.date;
}
elseif ($argv[1] == 1)
{
  $metrics = array (apollo_APACHE_external.r_per_sec,
apollo_APACHE_external.b_workers);
  $query = 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  apollo_APACHE_external.date = 2004010100 AND
apollo_APACHE_external.date = 2004093023 ORDER BY
apollo_APACHE_external.date;
}
elseif ($argv[1] == 2)
{
  $metrics = array (apollo_APACHE_external.r_per_sec,
apollo_APACHE_external.b_workers, apollo_APACHE_external.i_workers);
  $query = 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_workers
AS c2 FROM apollo_APACHE_external WHERE apollo_APACHE_external.date =
2004010100 AND apollo_APACHE_external.date = 2004093023 ORDER BY
apollo_APACHE_external.date;
}
else
{
  exit;
}

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

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

  # Build the y axis data
  for ($i=0; $i  count($metrics); $i++)
  {
    $datay[$i][$count] = $row[c$i];   # *KEY LINE*
  }
  $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 = array();

# Reassign newdatax back to datax
$datax = $newdatax;   # *KEY LINE*
my_log(Exiting);
?
*** CODE***

*** COMAND LINE OUTPUT ***
$ php -v
PHP 5.2.1 (cli) (built: May  3 2007 11:15:31)
Copyright (c) 1997-2007 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies
    with Zend Extension Manager v1.2.0, Copyright (c) 2003-2007, by Zend
Technologies
    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 

Re: [PHP-DB] Script 'hanging' for a long time before exit

2007-05-17 Thread Chris



$ 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