Re: Command-line PHP script CPU usage goes sky-high, stays there--why?

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

 



On 10-Dec-07, at 5:20 PM, Jim Lucas wrote:

Tom Rogers wrote:
Hi,

Tuesday, December 11, 2007, 6:42:18 AM, you wrote:
RF> Hello,

Put a usleep(1000) in the listen while() loop and give the cpu a
break.


This makes me think about asking if you have to short of a timeout on your receiving connection?

One second on stream_socket_server(), with a 900 second timeout stream_socket_accept().

What are you using to setup your connection? fsockopen() or stream_socket_server() ?

here is a snippet of what I have
[...]

Thanks for sharing your code. Seems pretty similar to mine at first glance.

I don't have a timeout set on the *_recvfrom() call. I just wait until the next connection comes in.

You don't need to use mysql_pconnect(), especially if you are using, what in essence is, a daemon.

Yeah, after thinking about it, that's what I figured. Thanks for confirming though.

Just don't open and close the connection constantly, leave it open.

Yes, I just open it once at the top of the script. And that's it.

Also, make sure you are not using an array that you are not re- initializing through each iteration of the loop. If the array keeps getting bigger, PHP might $*%& on itself. Always re-initialize
arrays to clean them up.

Hope some of this helps!

All good advice. I will check my arrays, although I don't think this is a problem since I monitor the scripts memory usage with memory_get_usage() and memory_get_peak_usage(), and it never tops 3MB (max allocated is 16MB). There are a few little parts to the daemon. One thing I'm doing that could be problematic is running an include (); on a couple files each time a socket has new data (this allows me to adjust the processing logic on the fly without having to start the script and wait for clients to reconnect)--but I can see this being expensive in terms of performance and resources. Actually, I wonder if THAT is not what's starving the script of resources over time-- each fread() involves several includes(); I'll have to look into that...

FWIW, here's the stripped-down skeleton of the server:
As always, constructive criticism is very welcome.

<?php

$socket = stream_socket_server("tcp://127.0.0.1:9876", $errno, $errstr);

if ($socket) {
	
	$master[] = $socket;
	$read = $master;
	$write = $master;			

	while (1) {
	
		$read = $master;
		$write = $master;			
		$mod_fd = stream_select($read, $_w = NULL, $_e = NULL, 1);

		if ($mod_fd === FALSE) {
			break;
			}
			
		for ($i = 0; $i < $mod_fd; ++$i) {
			if ($read[$i] === $socket) {		// NEW SOCKET

				$conn = stream_socket_accept($socket, 900);
				$master[] = $conn;
				$key_num = array_search($conn, $master, TRUE);
				
				} else {

				$sock_data = fread($read[$i], 32768);
				
				if (strlen($sock_data) === 0) { 	// CONNECTION GONE
				
					$key_to_del = array_search($read[$i], $master, TRUE);
					fclose($read[$i]);	
					unset($master[$key_to_del]);
					
					} elseif ($sock_data === FALSE) {	// CONNECTION BROKEN
					
					$key_to_del = array_search($read[$i], $master, TRUE);
					fclose($read[$i]);
					unset($master[$key_to_del]);

					} else {							// READ INCOMING DATA
										
					// include (somefiles);
					// include (somefiles);
					// include (somefiles);
					// [ ... ]

					}
				}
			}
		}
	}

?>

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