Php 将值返回给调用线程

Php 将值返回给调用线程,php,multithreading,Php,Multithreading,我正试图在PHP中使用pthreads来加速一系列的计算,这些计算目前最多只能使用一个CPU内核几秒钟。我已将计算拆分为多个范围,并在父线程中运行每个范围: $threads = array(); foreach($cp as $c) { $threads[$c] = new ThreadedMatcher($params); $threads[$c]->start(); } 然后,我希望array\u合并父线程中每个子线程(针对每

我正试图在PHP中使用pthreads来加速一系列的计算,这些计算目前最多只能使用一个CPU内核几秒钟。我已将计算拆分为多个范围,并在父线程中运行每个范围:

    $threads = array();
    foreach($cp as $c) {
        $threads[$c] = new ThreadedMatcher($params);
        $threads[$c]->start();
    }
然后,我希望
array\u合并父线程中每个子线程(针对每个范围)中创建的数组,以获得整个数据集的值

我推测我需要在父线程中使用
join()
来等待线程完成,但如何从子线程中实际获取一个值到父线程中?

尝试以下操作:

这将根据可用的内核划分负载,在批之间循环,不超过可用的CPU最大线程数。 我使用这个非常简单的原理,而不是我测试过的许多其他pthread可能性。到目前为止,对我来说最简单、最有效的一个

与氙气服务器和80个可用CPU线程完美配合,迭代次数超过100万次,持续数小时

$Pay_Load=array(
array('id'=>$id,'query'=>$query1,'param'=>$param1),
array('id'=>$id,'query'=>$query2,'param'=>$param2),
/*... can be hundreds...just an example...*/
)

$Nb_of_Cpus=4;
$Nb_of_threads=$Nb_of_Cpus*2;
$Batch_Works=array_chunk($Pay_Load,$Nb_of_threads);

$threads = [];
$results = array();

foreach ($Batch_Works as $batch) {
    foreach ($batch as $key => $params) {
        $threads[$key] = new ThreadedMatcher($params);
        $threads[$key]->start();
    }
    foreach ($batch as $key => $params) {
        $threads[$key]->join();
        $returned_result=$threads[$key]->result;
        $returned_id=$threads[$key]->id;
        $result=array('id'=>$returned_id,'result'=>$returned_result);
        array_push($results, result);    
    }
}
/* all returned results are now in the same array */
/* with the original Payload id as an example here */
var_dump($results);
全班:

class ThreadedMatcher extends Thread {

private $query;
private $param;
public $result;
public $id;


public function __construct($params) {

    $this->query = $params['query'];
    $this->param = $params['param'];
    $this->id= $params['id'];

}

public function run() {

    /* do some stuff*/
    echo ($this->query);
    echo ($this->param);

    $this->result = rand(100, 200);
}
}

$Pay_Load=array(
array('id'=>$id,'query'=>$query1,'param'=>$param1),
array('id'=>$id,'query'=>$query2,'param'=>$param2),
/*... can be hundreds...just an example...*/
)

$Nb_of_Cpus=4;
$Nb_of_threads=$Nb_of_Cpus*2;
$Batch_Works=array_chunk($Pay_Load,$Nb_of_threads);

$threads = [];
$results = array();

foreach ($Batch_Works as $batch) {
    foreach ($batch as $key => $params) {
        $threads[$key] = new ThreadedMatcher($params);
        $threads[$key]->start();
    }
    foreach ($batch as $key => $params) {
        $threads[$key]->join();
        $returned_result=$threads[$key]->result;
        $returned_id=$threads[$key]->id;
        $result=array($returned_id=>$returned_result);
        array_push($results, result);    
    }
}
/* all returned results are now in the same array */
/* with the original Payload id as an example here */

var_dump($results);

$results =(
1=>103,
2=>234,
3=>345,
4=>123)

这些行如何从ThreadedMatcher获取数据$返回的_result=$threads[$key]->result$返回的_id=$threads[$key]->id;对在类中将它们设置为公共。$this->id.$this->result。在运行class.PHP结束时,数组按值传递。这意味着整个数组必须在线程中(重新)分配,并在其连接上取消分配,这将大大降低处理线程时所寻求的性能。考虑将数组作为引用来替代。这个过程的秘密是,在准备有效载荷数组时,要确定每个工人将要计算的迭代次数。正确地对它们进行分类,并相应地将批处理工作分块。