Re: Executing functions or scripts in parallel
| From: | Adam Zey | Date: | Tue, 30 May 2006 20:27:46 +0000 |
| Subject: | Re: Executing functions or scripts in parallel | ||
| References: | 1 2 3 4 5 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-237108@lists.php.net to get a copy of this message | ||
Phil Martin wrote:
Many parts of this script were found at the following site: http://www.sitepoint.com/article/udp-portscanning-php This script isn't totally mine. I've just adapted it to my needs and it is working quite well, telling me when a service is up or down ... read the article for further details. Anyway, my question was about concurrent script running. My script is working the way I needed it to and all I need now is a way to fasten it. I still think it's a little bit slow by many aspects, maybe because I'm not a programmer and I've written a code that is not so good or fast, or there is no way to have all my servers services checked in parallel. Thanks in advance. Felipe Martins On 5/30/06, Jochem Maas <jochem@iamjochem.com> wrote:Phil Martin wrote:Sure, sorry about that. I have a function that tells me if the host isDOWNor UP. I want to run this function in parallel for each host I monitor.Thefunction is the following: function servstatus($remote_ip, $remote_ip_port, $remote_ip_proto) { if ($remote_ip_proto == "tcp") {$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);// Turning Off Warning Messages $i_error_old=error_reporting(); error_reporting($i_error_old & ~ (E_WARNING));if ($connect = socket_connect($socket, "$remote_ip", "" .$remote_ip_port . "")) {return true;the following 3 statements will never be run.
socket_set_block($socket);
socket_close($socket);
unset($connect);
} else {
return false;
the following 3 statements will never be run.
socket_set_nonblock($socket);
socket_close($socket);
unset($connect);}
my guess is you know even less about sockets, especially
blocking/non-blocking
than I do!
} else {
why are you making the distinction between udp and tcp in this way?
// Turning off Warning Messages
$i_error_old=error_reporting();
error_reporting($i_error_old & ~ (E_WARNING));
$socket = fsockopen("udp://$remote_ip", "$remote_ip_port",
&$errno,
&$errstr, 2);
$timeout = 1;
if (!$socket) {
if you don't get the socket why does that mean the service is up?
return true;
}
socket_set_timeout ($socket, $timeout);
$write = fwrite($socket,"\x00");
if (!$write) {
again if you can't write why does that mean the service is up?
return true;
}
$startTime = time();
$header = fread($socket, 1);
$endTime = time();
$timeDiff = $endTime - $startTime;
if ($timeDiff >= $timeout) {
why does a time measurement mean the service is up?
fclose($socket);
return true;
} else {
fclose($socket);
return false;
}
}
}
Thanks in advance
Felipe Martins
On 5/30/06, Dave Goodchild <buddhamagnet@gmail.com> wrote:
Hi everyone,On 30/05/06, Phil Martin <darkinner.lists@gmail.com> wrote:I've made a very basic and small function to suit my needs inmonitoring some hosts services. I've noticed that the script is abit slow because of the number of hosts and services to monitor. Is there a way to execute my function servstatus(); in parallel with every hosts to increase the speed ?littleThanks in advance.[]'s Felipe Martinscss)Can you show us the function? -- http://www.web-buddha.co.uk dynamic web programming from Reigate, Surrey UK (php, mysql, xhtml,look out for project karma, our new venture, coming soon!
This is all in the manual... Essentially you want pcntl_fork(), although you might want to use something like stream_socket_pair() to do interprocess communication. You should be able to find all the documentation and examples you need in the manual in the pcntl and stream sections. Regards, Adam Zey.