php exec() 在 pthreads 中随意工作

php exec() haphazardly working in pthreads

我在 pthreads 中使用 for 循环 exec() 一个文件 300 次。有时完整的 300 次 exec() 调用都是成功的,但大多数时候有一些 exec() 失败,我得到了 295 或 299 次成功执行文件。

exec() 的错误代码总是在#127 处返回, 我检查了 file_exists 失败,它总是显示 "file exists",但它也显示 "file is not executable" 失败。这很奇怪,因为它在循环中执行了另外 295 次。

我的 pthread 版本只有 2 个月大。我非常有信心我没有任何 pthread 工作人员共享文件或写入相同的位置。摸不着头脑,还有什么可以做的。

class WorkerThreads extends Thread
{
    private $workerId;

    public function __construct($id)
    {
        $this->workerId = $id;
    }

    public function run()
    {

$mainfile="/pastlotto_1/".$this->workerId."/preset_pthread.php";

        for($x=0; $x<100; $x++)
{

    for($o=0; $o<3; $o++)
    {
        $command="php $mainfile";
        $findE=exec($command,$output,$return_var);

        if($return_var !== 0){ 

    echo " file not executed at worker id ".$this->workerId."\n";
    echo $return_var."\n";

    if (file_exists($mainfile)) {
    echo "The file $mainfile exists\n";
} else {
    echo "The file $mainfile does not exist\n";
}
  if(is_executable($mainfile))
  {
  echo ("file is executable\n");
  }
else
  {
  echo ("file is not executable\n");
  }
 }
else{
    echo "File executed Successfully";
}
    }
}
        }//END OF RUN

}

for($r=0; $r<1; $r++)
{
$workers = [];

// Initialize and start the threads
foreach (range(0,2) as $j) {

    $workers[$j] = new WorkerThreads($j);
    $workers[$j]->start();


    //echo $i." worker started\n";
}

// Let the threads come back
foreach (range(0,2) as $j) {
    $workers[$j]->join();

}
unset($workers);

}

我能够通过更改启动 pthreads 的代码来解决问题:

for($r=0; $r<1; $r++)
{
$workers = [];

// Initialize and start the threads
foreach (range(0,2) as $j) {

    $workers[$j] = new WorkerThreads($j);
    $workers[$j]->start();


    //echo $i." worker started\n";
}

// Let the threads come back
foreach (range(0,2) as $j) {
    $workers[$j]->join();

}
unset($workers);

}

替换代码:

$p = new Pool(3);



// Initialize and start the threads
 for($b=0; $b<3; $b++) {
        $tasks[$b]= new WorkerThreads($b);


    }


  // Add tasks to pool queue
    foreach ($tasks as $task) {
        $p->submit($task);
    }

    // shutdown will wait for current queue to be completed
    $p->shutdown();