我从PHP调用TCL脚本。我从TCL进程向PHP发送了一个唯一的字符串,以确保脚本已经结束。
如果我不发送该字符串,那么我在PHP中的fread将永远被阻塞。
// PHP代码
<?php
$id = 'done'; //Unique string
$app = 'c:/wamp/www/tcl/bin/tclsh84.exe';
$descriptorspec = array(
0 => array("pipe","r"),
1 => array("pipe","w"),
2 => array("pipe","w")
) ;
$process = proc_open($app, $descriptorspec, $pipes);
if (is_resource($process))
{
for($i=0;$i<2;$i++)
{
$output = '';
$continue = true;
$cTimeout = 0;
echo 'loop ', $i, "\n";
fwrite($pipes[0], "source c:/wamp/www/tcl/bin/helloworld.tcl\n");
echo "waiting for idle\n";
$timeout = time();
do {
$read=array($pipes[1]);
$write=array();
$except=array($pipes[1]);
$ready = stream_select($read, $write, $except, 1, 0);
$dif = time()- $timeout;
if ( $ready && $read )
{
$output .= fread($pipes[1], 2048); // is blocked indefinitely
// if the delimiter id shows up in $output
if ( false!==strpos($output, $id) ) {
// the script is done
$continue = false;
}
}
if($dif > 5) //timeout value not working
{
$continue = false;
}
} while($continue);
echo 'loop ', $i, "$output finished\n";
}
proc_close($process);
}
?>//TCL代码
放上"hello“
如果我从TCL发送"done“,那么我的PHP脚本结束。但我不想发送刚刚完成,而是需要在超时的帮助下做。也就是说,我想为唯一的字符串等待一段时间,否则我应该退出。但在这种情况下,我似乎无法实现超时。
请大家给我指点一下。
发布于 2009-11-03 19:18:16
你必须重新思考你的程序的逻辑,但你可以:
你的脚本应该是这样的
// sets the maximum execution time (seconds)
set_time_limit(3);
function shutdown () {
// if the script fails some logic goes here
}
// registers the function to run on shutdown
register_shutdown_function('shutdown');这应该会让你走上正确的方向。
希望它能帮上忙!
https://stackoverflow.com/questions/1666453
复制相似问题