fread için zaman aşımı

1 Cevap

Ben PHP TCL komut arıyorum. Ben emin komut sona erdi yapmak için PHP TCL sürecin benzersiz bir dize gönderiyorum.

Ben bu dizeyi göndermek yoksa o PHP benim fread sonsuza kadar bloke edilir.

/ / PHP kodu

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

"merhaba" koyar

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.

Kimse bana rehberlik edin.

1 Cevap

Eğer programın mantığını yeniden düşünmeye ama sen yapabilirsin gerekir:

  1. (PHP çalışır işlevi çıkmak için olduğu gibi) kapatma için bir işlev Ol
  2. Max yürütme zamanlı sınırı ayarlayın

Ve komut böyle gider

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

Bu doğru yönde size ayarlamanız gerekir.

Umarım yardımcı olur!