私はまだPHPに比較的慣れておらず、pthreadsを使用して問題を解決しようとしています。さまざまな時間に終了するプロセスを実行する 20 のスレッドがあります。ほとんどの場合、約 10 秒未満で終了します。20 個すべてが必要なわけではなく、検出されたのは 10 個だけです。10 に達したら、スレッドを強制終了するか、次のステップに進みたいと思います。
各スレッドで set_time_limit を約 20 秒に設定しようとしましたが、スレッドはそれを無視して実行を続けます。プログラムの残りの部分を実行したくないので、結合を探してジョブをループしていますが、最も遅いものが終了するまで立ち往生しています。pthreads によって時間は約 1 分から約 30 秒に短縮されましたが、最初の 10 回の実行から約 3 秒でさらに時間を短縮できました。
助けてくれてありがとう、ここに私のコードがあります:
$count = 0;
foreach ( $array as $i ) {
$imgName = $this->smsId."_$count.jpg";
$name = "LocalCDN/".$imgName;
$stack[] = new AsyncImageModify($i['largePic'], $name);
$count++;
}
// Run the threads
foreach ( $stack as $t ) {
$t->start();
}
// Check if the threads have finished; push the coordinates into an array
foreach ( $stack as $t ) {
if($t->join()){
array_push($this->imgArray, $t->data);
}
}
class class AsyncImageModify extends \Thread{
public $data;
public function __construct($arg, $name, $container) {
$this->arg = $arg;
$this->name = $name;
}
public function run() {
//tried putting the set_time_limit() here, didn't work
if ($this->arg) {
// Get the image
$didWeGetTheImage = Image::getImage($this->arg, $this->name);
if($didWeGetTheImage){
$timestamp1 = microtime(true);
print_r("Starting face detection $this->arg" . "\n");
print_r(" ");
$j = Image::process1($this->name);
if($j){
// lets go ahead and do our image manipulation at this point
$userPic = Image::process2($this->name, $this->name, 200, 200, false, $this->name, $j);
if($userPic){
$this->data = $userPic;
print_r("Back from process2; the image returned is $userPic");
}
}
$endTime = microtime(true);
$td = $endTime-$timestamp1;
print_r("Finished face detection $this->arg in $td seconds" . "\n");
print_r($j);
}
}
}