PHP - 使用pthreads多线程函数

时间:2015-06-01 13:53:34

标签: php multithreading 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);

任何帮助都将不胜感激。

详细说明:

  • my_function输出UTF-8字符串。

2 个答案:

答案 0 :(得分:2)

基本问题是数组不是线程安全的,pthreads在所有Threaded对象上提供类似数组的接口;这意味着您可以在多线程上下文中使用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);
?>

没有内置的方法来进行多线程排序,所以最简单的方法是在线程完成时对结果进行排序。

答案 1 :(得分:1)

我确信有人可以做得更好,但它(显然)正在工作,并且它的性能得到了极大提升。 &#34;等待线程&#34;部分是非常低效和不优雅的,任何帮助将不胜感激!

首先,检查您是否使用phpinfo()安装了pthread,或者安装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!享受!