PHP5 Script 'Freeze' before exiting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting PHP5 Script 'Freeze' before exiting
# 1  
Old 05-10-2007
PHP5 Script 'Freeze' before exiting

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).

After putting various debug statements in I found that if I commented out the following line the script didn't hang before exiting.

$datax = $newdatax;

A pretty innocuous line. At this point $datax is a large array (many thousands of values long), and $newdatax an array with the results from parsing the original $datax in some way (mostly a case of taking every Nth entry from $datax). At this point I wish to throw away the original $datax and replace it with the new $newdatax and I may wish to do more maniplulation later.

If $datax is about 10000 or so rows there is a noticeable hang when the script exits, at 50000 rows the hang is over 20 seconds. Under php 4 I had no problems with this code when there were over 200,000 rows in the array.

Even if I simply change that line to

$datax = array();

I get similar (but much smaller) 'hang' before the script exits and the $datax array needs to be quite large before it becomes noticeable.

System is running on Solaris 9 on a v480 with 4 processors and 16GB of RAM. I've tried it with and without the Zend optimiser (v3.2.8) with no noticeable difference.

Any help appreciated.
# 2  
Old 05-10-2007
Have you closed the DB connection immediately when you are done with the database or statement handles etc.? If you are running a resource-intensive script, you should make sure you deallocate things as quickly as you can to avoid holding something for too long. You should not rely on the automatic reference-count garbage collection in this case as it is possible that deallocation may not occur soon enough.

If you are having a variable with very large content, you should consider manually unset() the variables immediately when you are done with it. It may not be related to your case but just try.
# 3  
Old 05-10-2007
Sorry forgot to say, I explicitly added mysql_free_result and mysql_close calls to make sure it wasnt that. I've tried doing

unset($datax);

before re-assigning it and I get the same 'hang'.
# 4  
Old 05-10-2007
Not too sure about that.

In fact I made a simple script:

Code:
<?php

$ary = range(0, 50000);
$newary = $ary;

?>

So I am assigning a 50001-element array to another variable. There doesn't seem to be any speed issue on my PHP 5.2.0. It's just a desktop system.

But are you sure of the changes between PHP4 and PHP5 regarding variable/object assignment (copying) behaviour? As you cannot provide with the simplest version of the script that give the behaviour, I guess everything mentioned can just be a wild guess.
# 5  
Old 05-10-2007
Ok I have the code down to about the smallest I can and still show you what is happening. The include.php is simply the databsae 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.

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 >= 20040101000000 AND apollo_APACHE_external.date <= 20040930230000 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 >= 20040101000000 AND apollo_APACHE_external.date <= 20040930230000 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 >= 20040101000000 AND apollo_APACHE_external.date <= 20040930230000 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");
?>

Ouput from my system when running the above script 3 times:

Code:
$ 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 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
$

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help for exiting the function not script

function2() { cmd1 cmd2 cmd3 .... cmdn } function2() { cmd11 cmd12 cmd13 .... .... } for i in {1,2} (7 Replies)
Discussion started by: yanglei_fage
7 Replies

2. SCO

Logout automatically when exiting a script

Hi, I am using SCO Unix. I write a script to execute a program by calling exec program The script will check the program is still there or not, if not, then it will exit the script by calling exit So if I press DEL to quit the program, the script will exit, but it will logout... (6 Replies)
Discussion started by: dannychan
6 Replies

3. Shell Programming and Scripting

Exiting from the script abruptly

Hi Team, Need your help for the below code snippet. I wrote a module to read the file names remote server using file name convention. Issue : My script is coming out from while loop without reading complete file. test1() { while read line do echo $line file_nm_convention=`echo... (3 Replies)
Discussion started by: gvkumar25
3 Replies

4. Shell Programming and Scripting

Exiting out of the script

I have to write a script in ksh which again should call another script. Say A.ksh is calling B.ksh. Now in B.ksh if the condition we are checking for is true then we have to go back to the main script A.ksh or if the condition in B.ksh is false then we have to totally come out of the scripts. I... (1 Reply)
Discussion started by: vpv0002
1 Replies

5. Shell Programming and Scripting

Bash Script Not Exiting

I have a script planned for the Helpdesk to allow them to (on a couple of RHEL 3 / RHEL 5 servers) reset passwords, stop / start printers, and clear print queues. The appropriate sudo permissions were given to their accounts, and the individual functions all work just fine. The ability to move... (0 Replies)
Discussion started by: tearsong
0 Replies

6. Shell Programming and Scripting

exiting from script

Hi, I am trying to exit the script from a function. I was in assumption that if we use exit ( inside or outside the function) it will exit from the script. alternatively, return will exit from that particular function. but in my case, exit is exiting from the function and not the script.... (8 Replies)
Discussion started by: shellwell
8 Replies

7. Shell Programming and Scripting

shell script exiting before completing

I have a script which has the following statement, /opt/oracle/product/9i/bin/sqlplus << EOC >> $LOG_FILE 2>&1 username/password ---- Enters the SQL prompt @/export/home/oracle/shells/grant_userview.sql ---Runs the SQL script @/export/home/oracle/shells/grant_proc_userview.sql ---Runs the... (6 Replies)
Discussion started by: welldone
6 Replies

8. Shell Programming and Scripting

Exiting a script

I have a script abc.sh. Its contents are as follows: (7 Replies)
Discussion started by: lassimanji
7 Replies

9. Shell Programming and Scripting

exiting from script

there are many script in my project.i am having a problem when i am trying to quit from child script.what is the command to wrap up all the parent script and calling script as well? exit 0 is not working.please help.... (1 Reply)
Discussion started by: arghya_owen
1 Replies

10. Shell Programming and Scripting

Exiting from script when error occurs

Hi Friends, Is it possible to exit nicely(ie, to echo a message with the error occurred) from a shell script(quiet a big one :)) once it encounter an error in between the lines? For example, in my script I am calling the command mkdir and sometimes (when the directory already exists) it... (4 Replies)
Discussion started by: Sreejith_VK
4 Replies
Login or Register to Ask a Question