John Wards wrote:
On Mon, 2004-09-20 at 16:56, Marek Kilimajer wrote:

play around with popen() and stream_select()


Oooh that looks handy.

More night time reading I think!

Any further ideas to save a man with a cold from using his brain would
be great!

Cheers
John


Wanted to do something interesting, so here is something to get you started. It opens a pool of 10 sleep processes that sleep for a random time. Everytime a sleep process ends its sleep and exits, a new sleep process is created.


However, it contains some very, very ugly hack. stream_select() breaks index association of its parameters, so to find out what stream changed status one has to compare values of the original array ($handles) and the array passed to stream_select() ($read).

Is it how it's supposed to work or am I missing something?

<?php

$handles = array();
for($i = 0; $i < 10; $i++) {
  $handles[$i] = popen('sleep ' . rand(1, 10), 'r');
  echo "Opened '$handles[$i]'\n";
}

while(count($handles)) {

$read = $handles;
if (false === ($num_changed_streams = stream_select($read, $write = NULL, $except = NULL, 10))) { // don't forget to change tv_sec param
/* Error handling */
echo "ERROR\n";
} elseif ($num_changed_streams > 0) {


     foreach($read as $key => $handle) {
       echo "Closing '$handle'\n";
       //echo fread($handle, 2096);
       pclose($handle);
       if($i < 50) {
         $handles[$i] = popen('sleep ' . rand(1, 10), 'r');
         echo "Opened '$handles[$i]'\n";
         $i++;
       }
     }
     // UGLY hack here
     $handles = array_diff($handles, $read);
  }
}

?>

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



Reply via email to