Use of callback on a stream
| From: | Olivier Dupuis | Date: | Fri, 28 Mar 2008 13:08:13 +0000 |
| Subject: | Use of callback on a stream | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-272216@lists.php.net to get a copy of this message | ||
Hello,
I am trying to make a daemon that launches shell commands via proc_open and gets the stdout of the command via a pipe. The thing is that I would like to get this stdout via a callback, instead of monitoring the pipe regularly.
I tried the following code (this is a simplified version of it, but the callback never gets called,
does anyone know what I forgot ?
Thanks,
Olivier
<?php
// First we proc_open a ls
// 1 - we build the descriptor array$descriptorspec = array(
0 => array("pipe", "r"), // // stdin
1 => array("pipe", "w"), // stdout
2 => array("pipe", "w") // stderr
);
$pipes=array();
// We make a simple callback function
function stream_callback(){
global $pipes;
$stdout_child=stream_get_contents($pipes[1]);
print("Callback received:\n");
print($stdout_child."\n");
}
$context=stream_context_create(array());
stream_context_set_params($context,array('notification' => 'stream_callback'));
$handle=proc_open("/bin/ls /tmp",$descriptorspec,$pipes,null,null,array('context' => $context));
stream_context_set_params($pipes[1],array('notification' => 'stream_callback'));
while (true){
print("Waiting...\n");
sleep(5);
}