PHP问题的Pthread

时间:2017-08-26 09:39:28

标签: php multithreading parallel-processing pthreads

我在php中使用PThreads进行多线程处理。我已在Windows上的XAMPP服务器上成功安装并运行它。 我的数据库中有100K记录,我希望并行运行20个线程。每个线程都会从数据库调用5k记录并处理它们。这是我的代码

require('mailscript.php');
class My extends Thread{

    function __construct() {
        $this->mailscript = new mailscript();
     }

    function run(){
        $this->mailscript->runMailScript(5000);
    }
}

for($i=0;$i<20;$i++){
    $pool[] = new My();
}

foreach($pool as $worker){
    $worker->start();
}
foreach($pool as $worker){
    $worker->join();
}

当我运行此代码时,每个线程最多只运行约600条记录。对于PThreads中的线程数有任何限制问题。有什么问题请帮帮我

1 个答案:

答案 0 :(得分:1)

但是,这就是我如何用你的例子处理pthreads的方法,如果有必要,可以使用一个集合方法。希望这会对你有所帮助。

/*pthreads batches */
$batches = array();

$nbpool = 20; // cpu 10 cores 

/* job 1 */
$list = [/* data1 */];
$batches[] = array_chunk($list, 5000);

/* job 2 */
$list2 = [/* data2 */];
$batches[] = array_chunk($list2, 10000);

/*final collected results*/
$resultFinal = [];

/* loop across batches */
foreach ($batches as $key => $chunks) {

    /* for intermediate collection */
    $data[$key] = [];

    /* how many workers */
    $workCount = count($chunks);

    /* set pool job up to max cpu capabilities */
    $pool = new Pool($nbpool, Worker::class);

    /* pool cycling submit */
    foreach (range(1, $workCount) as $i) {
        $chunck = $chunks[$i - 1];
        $pool->submit(new processWork($chunck));
    }

    /* on collection cycling */
    $collector = function (\Collectable $work) use (&$data) {

        /* is worker complete ? */
        $isGarbage = $work->isGarbage();

        /* worker complete */
        if ($isGarbage) {
            $data[$key] = $work->result;
        }
        return $isGarbage;
    };
    do {
        /* collection on pool stack */
        $count = $pool->collect($collector);
        $isComplete = count($data) === $workCount;
    } while (!$isComplete);

    /* push stack results */
    array_push($resultFinal, $data);

    /* close pool */
    $pool->shutdown();
}

class processWork extends \Threaded implements \Collectable {

    private $isGarbage;
    private $process;
    public $result;

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

    public function run() {
        $workerDone = array();
        foreach ($this->process as $k => $el) {
            /* whatever stuff with $this->process */
        }
        $this->result = $workerDone;
        $this->isGarbage = true; // yeah, it s done
    }

    public function isGarbage(): bool {
        return $this->isGarbage;
    }
}