2017-07-25 66 views
0

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

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

} 

回答

0

我能够通过改变从激发了并行线程的代码来解决这个问题:

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

} 

置换CODE:

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