2016-10-14 11 views
2

私は、スレッドを管理するためにワーカーを使用してマルチスレッドを使用することに頼っていたので、PHPでプロセスを改善しています。しかし、作業者のスレッドは、同時に実行されないスレッドを順番に実行します。PHP - PTHREADワーカースレッドが同時に実行されていません

これは私のサンプルコード

namespace App\Engine\Threads\ThreadClass; 


class StatePaperAttendancePDFThread extends \Threaded 
{ 
    private $write_folder_name; 
    private $center_code; 
    private $paper_id; 
    private $title; 
    public function __construct($write_folder_name, $center_code, $paper_id, $title) 
{ 
    $this->write_folder_name = $write_folder_name; 
    $this->center_code = $center_code; 
    $this->paper_id = $paper_id; 
    $this->title = $title; 

} 


public function run(){ 

    echo " Retrieving paper attendance ".$this->center_code." ".$this->paper_id." ".\Thread::getCurrentThreadId()." ".date("H:i:s:u").PHP_EOL; 
    $artisan = 'C:/xampp/htdocs/attendance/artisan'; 
    $cmd = "php $artisan attendancereport:center $this->write_folder_name $this->center_code $this->paper_id $this->title"; 
    $return = exec($cmd, $output, $return_var); 
    echo $return; 

} 
} 


foreach ($centers as $i=>$center){ 
     $center_code = $center->getCenterCode(); 
     $thread = new StatePaperAttendancePDFThread($folder_name, $center_code, $paper_id, $title); 
     $worker->stack($thread); 
    } 

$worker->start(PTHREADS_INHERIT_ALL^PTHREADS_INHERIT_CLASSES); 
$worker->shutdown(); 

ですが、私は時間を使用してCLIからそれを監視するとき、私は、スレッドのどれもが一緒に起動しないことがわかります印刷されて。彼らはすべて私は以下のようにカスタマイズされたスレッドプールエグゼキュータを作成することで、私の問題を解決することができた私は

+0

私が理解する限り、ワーカーはスタックされた 'Threaded'オブジェクト(Stackable)を1つのスレッドで実行します。 f.eを参照してください。 http://stackoverflow.com/questions/15955886/what-is-the-difference-between-thread-object-and-worker-object-php-pthreads。可能であれば、[Thread/Worker Pool](http://stackoverflow.com/a/23602185/3828957)か、スレッドを '$ thread-> start'で起動する必要があります。私は具体的な知識を持っていないので、これはもっとアイデアで答えはありません。 – makadev

+0

ありがとう@makadevプールを使用して私のために動作しません私はスレッドの開始にいくつかのパラメータを渡す必要があり、プールは開始機能を持っていない –

答えて

1

をしないのです間隔数秒で始まる

してください。私は提案と改善にご意見をお聞かせください。

class ThreadPoolExecutor{ 
    private $poolSize; 
    private $threadPool; 
    private $done = false; 
    private $workingThreads; 

public function __construct($poolSize, array $threadPool) 
{ 
    $this->poolSize = $poolSize; 
    $this->threadPool = $threadPool; 
} 

public function execute() 
{ 
    $this->parametersOk(); 

    try { 
     while (!empty($this->threadPool)) { 
      $this->extractThreads(); 

      foreach ($this->workingThreads as $thread) { 
       $thread->start(PTHREADS_INHERIT_ALL^PTHREADS_INHERIT_CLASSES); 
      } 

      foreach ($this->workingThreads as $thread) { 
       $thread->join(); 
      } 

     } 
     $this->done = true; 
    } catch (\Exception $ex) { 

     var_dump($ex->getMessage()); 
    } 

} 


private function parametersOk() 
{ 
    if (!is_array($this->threadPool)) 
     throw new \RuntimeException("threadPool expected to be an array of threads"); 
    if (count($this->threadPool) <= 0) 
     throw new \RuntimeException("expected at least an element in threadPool"); 

    foreach ($this->threadPool as $thread) { 
     if (!is_subclass_of($thread, \Thread::class, false)) { 
      throw new \RuntimeException(" an element of threadPool does not extend class \\Thread"); 
     } 

    } 

    if ($this->poolSize > count($this->threadPool)) { 
     throw new \RuntimeException("The number of threads set to execute can not be greater than the threadPool"); 
    } 

    return true; 
} 

private function extractThreads() 
{ 
    $this->workingThreads = []; 
    $this->workingThreads = array_slice($this->threadPool, 0, $this->poolSize); 

    for ($i = 0; $i < count($this->workingThreads); $i++) { 
     array_shift($this->threadPool); 
    } 
} 

public function isDone() 
{ 
    return $this->done; 
} 


} 

これには追加または修正があります。

関連する問題