PHP - 使用 pthreads 多线程函数

PHP - Multithread a function with pthreads

我目前正在使用 pthreads 在要求非常高的函数上实现多线程。到目前为止,我已经开始工作了:

class Operation extends Thread {
  public function __construct($arg) {
    $this->arg = $arg;
  }  
  public function run() {
    if ($this->arg) {
      $parameters = $this->arg;
      echo my_function($parameters[0],$parameters[1]);
    }
  }
}
$stack = array();
foreach ($work as $operation) { $stack[] = new Operation($operation); };
foreach ($stack as $t) { $t->start(); };

直接输出结果。 我想将我的结果一个一个地存储在一个数组中(以相同的顺序会很好)但这当然行不通:

class Operation extends Thread {
  public function __construct($arg) {
    $this->arg = $arg;
  }  
  public function run() {
    if ($this->arg) {
      $parameters = $this->arg;
      $results[] = my_function($parameters[0],$parameters[1]);
    }
  }
}
$stack = array();
foreach ($work as $operation) { $stack[] = new Operation($operation); };
foreach ($stack as $t) { $t->start(); };
var_dump($results);

如有任何帮助,我们将不胜感激。

详情:

我相信有人可以做得更好,但它(显然)在工作,而且它大大提高了性能。 "waiting for thread" 部分非常低效且不优雅,我们将不胜感激!

首先,检查您是否使用 phpinfo() 安装了 pthreads 或安装它 https://php.net/manual/en/pthreads.installation.php

$key = 0;//We initialise the key to sort our results
foreach($iteration as $parameters) {//We make a loop to create the task list 
  $arguments[] = array($key,$parameters,$_POST['stuff'],$another_parameter,...);//We index ALL the parameters our function need for each task ($_POST, $_FILES, $_GET, user defined...) in a nice array
  ++$key;//We increment our key
};

class operation extends Thread {
  public $done = 0;//We initialize the "done" indicator
  public function __construct($arguments) {
    $this->arguments = $arguments;//We put our parameters in a construct
  }
  public function run() {
    if ($this->arguments)
    {
      $parameters = $this->arguments;//We extract the parameters for this worker
      $this->result = array($parameters[0], your_function($parameters[1],$parameters[2],...));//We launch our function and add the key to the result
      $this->done = 1;//This thread is done
    }
  }
}

$stack = array();//Lets initialize our stack
foreach ($arguments as $operation) { $stack[] = new operation($operation); };//We initialize the process
foreach ($stack as $t) { if($t->start()) { $t->join(); }; };//We launch it and wait until all the workers have completed their task

foreach($stack as $strata) {//We get each of the results
  while($strate->done == 0) {sleep(1);};//Inefficient way to wait for the thread to complete, ANY HELP WILL BE APPRECIATED
  $results[] = $strata->result;//Once it's done we add it to our results array
};
ksort($results);//We sort our array with our key
foreach($results as $line) { $results_sorted[] = $line[1]; };//We delete the key

现在你有了你的 $results_sorted !享受!

基本问题是数组不是线程安全的,pthreads 在所有 Threaded 对象上提供类似数组的接口;这意味着您可以在多线程上下文中使用线程对象代替数组。

<?php

function demanding(...$params) {
  /* you have parameters here */
  return array(rand(), rand());
}

class Task extends Collectable {
  public function __construct(Threaded $result, $params) {
    $this->result = $result;
    $this->params = $params;
  }

  public function run() {
    $this->result[] = 
      demanding(...$this->params);
  }

  protected $result;
  protected $params;
}

$pool = new Pool(16);

$result = new Threaded();

while (@$i++<16) {
  $pool->submit(
    new Task($result, $argv));
}

$pool->shutdown();

var_dump($result);
?>

没有执行多线程排序的内置方法,因此最简单的做法是在线程完成后对结果进行排序。