tags:

views:

118

answers:

1

Hello All,

I am calling TCL script from PHP. I am sending a unique string from TCL process to PHP to make sure that script has ended .

If I don't send that string then my fread in PHP is blocked forever .

// PHP code

<?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 code

puts "hello"

If i sends "done" from TCL, then my PHP script ends . But I don't want to send just done, instead I need to do with the help of a timeout . i.e I want to wait for a certain period of time for the unique string , else I should exit . But I can't seem to implement the timeout in this case.

Please can anyone guide me .

A: 

You'll have to rethink the logic of your program but you can:

  1. Register a function for shutdown (as PHP is about to quit runs that function)
  2. Set the max execution-time-limit

And your script would go like this

// 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');

This should set you on the right direction.

Hope it helps!

Frankie
but this is in the case of script failing . But in my script the fread is blocked as it has no data in buffer . hence it can't come out . The script ends only when I kill the tclsh process.
mithunmo
Failing as in going over it's execution time. Won't PHP just quit after a pre-defined amount of time waiting from TLC?
Frankie