Multithreading php exec()随意在pthreads中工作

Multithreading php exec()随意在pthreads中工作,multithreading,exec,Multithreading,Exec,我使用for循环在pthreads中执行文件300次。有时,300次完整的exec()调用是成功的,但大多数情况下,少数exec()调用失败,我成功执行了295次或299次文件 exec()的错误代码总是返回到#127, 我检查了失败时文件_是否存在,它总是说“文件存在”,但失败时它也说“文件不可执行”。这很奇怪,因为它在循环中的其他295次都是可执行的 我的pthread版本只有2个月。我非常有信心,我没有任何pthread工作人员共享文件或在同一地点写入。用我还能做的事来搔我的头 class

我使用for循环在pthreads中执行文件300次。有时,300次完整的exec()调用是成功的,但大多数情况下,少数exec()调用失败,我成功执行了295次或299次文件

exec()的错误代码总是返回到#127, 我检查了失败时文件_是否存在,它总是说“文件存在”,但失败时它也说“文件不可执行”。这很奇怪,因为它在循环中的其他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);

}
类WorkerThreads扩展线程
{
私人$workerId;
公共函数构造($id)
{
$this->workerId=$id;
}
公共功能运行()
{
$mainfile=“/passlotto_1/”$this->workerId。“/preset_pthread.php”;
对于($x=0;$xjoin();
}
未结算(工人);
}

我可以通过更改触发pthread的代码来解决这个问题:

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

}
$r=0;$rstart()的
;
//echo$i“工作人员已启动\n”;
}
//让线回来
foreach(范围(0,2)为$j){
$workers[$j]->join();
}
未结算(工人);
}
更换代码:

$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();
$p=新池(3);
//初始化并启动线程
对于($b=0;$b提交($task);
}
//关机将等待当前队列完成
$p->shutdown();