## Async I/O Èä³»³»±â
-------------------------------
<?php
##
## -- san2@2009.02.03
## -- imitative Async I/O non-block execution
## imitative Async I/O run execution
$proc[] = popen('sh ss.sh 8 2>&1','r');
$proc[] = popen('sh ss.sh 10 2>&1','r');
$proc[] = popen('sh ss.sh 20 2>&1','r');
## none-block
foreach($proc AS $fp)
{
stream_set_blocking($fp,0);
$stream[] = $fp;
}
print_r($stream);
while($read = $stream)
{
if(stream_select($read,$w=NULL,$e=NULL,0,500000) === FALSE) continue;
foreach($read AS $fp)
{
//while($buf = fgets($fp,512)) echo $buf;
fpassthru($fp);
if(feof($fp))
{
$key = array_search($fp,$stream);
unset($stream[$key]);
fclose($fp);
}
}
}
exit;
?>
-------------------------------
<?php
## -- san2@2009.02.03
## -- imitative Async I/O non-block execution
## 0=>stdin, 1=>stdout, 2=>stderr
$desp = array(0=>array('pipe','r'), 1=>array('pipe','w'), 2=>array('pipe','w'));
## imitative Async I/O run execution
$proc[] = proc_open('sh ss.sh 8',$desp,$p[]);
$proc[] = proc_open('sh ss.sh 10',$desp,$p[]);
$proc[] = proc_open('sh ss.sh 20',$desp,$p[]);
## set non-block
foreach($p AS $fp)
{
fclose($fp[0]);
stream_set_blocking($fp[1],0);
$stream[] = $fp[1];
}
print_r($stream);
while($read = $stream)
{
if(stream_select($read,$w=NULL,$e=NULL,0,500000) === FALSE) continue;
foreach($read AS $fp)
{
//while($buf = fgets($fp,512)) echo $buf;
fpassthru($fp);
if(feof($fp))
{
$key = array_search($fp,$stream);
unset($stream[$key]);
fclose($fp);
fclose($p[$key][2]);
proc_close($proc[$key]);
}
}
}
exit;
?>
-------------------------------
<?php
## -- san2@2009.02.03
## -- imitative Async I/O non-block execution, simple but not good
## 0=>stdin, 1=>stdout, 2=>stderr
$desp = array(0=>array('pipe','r'), 1=>array('pipe','w'), 2=>array('pipe','w'));
## imitative Async I/O run execution
$proc = proc_open('/bin/sh -i',$desp,$fp); // zombie
## set non-block
stream_set_blocking($fp[0],0);
stream_set_blocking($fp[1],0);
fwrite($fp[0],'/bin/sh ss.sh 12 2>&1 &');
fwrite($fp[0],'/bin/sh ss.sh 8 2>&1 &');
fwrite($fp[0],'/bin/sh ss.sh 20 2>&1 &');
fclose($fp[0]);
$stream = array($fp[1]);
while(1)
{
if(stream_select($stream,$w=NULL,$e=NULL,0,500000) === FALSE) continue;
while($buf = fgets($fp[1],512)) echo $buf;
//fpassthru($fp[1]);
if(feof($fp[1]))
{
fclose($fp[1]);
fclose($fp[2]);
proc_close($proc);
break;
}
}
exit;
?>
-----------------------------------------
|