CakeFest 2024: The Official CakePHP Conference

Pool::__construct

(PECL pthreads >= 2.0.0)

Pool::__construct创建新的 Worker 对象池

说明

public Pool::__construct(int $size, string $class = ?, array $ctor = ?)

创建新的 Worker 对象池,但是所对应的线程是延迟创建的的,也就是说, 直到需要执行任务的时候 才会创建对应的线程。

参数

size

此 Pool 对象可创建的 Worker 对象的最大数量

class

新创建的 Worker 对象的类。 如果不指定类,那么会使用默认的 Worker 类。

ctor

创建 Worker 对象时所用到的参数,以数组方式传入

示例

示例 #1 创建 Pool 对象

<?php
class MyWorker extends Worker {

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

public function
run() {
/** ... **/
}
}

$pool = new Pool(8, \MyWorker::class, [new Something()]);

var_dump($pool);
?>

以上示例会输出:

object(Pool)#1 (6) {
  ["size":protected]=>
  int(8)
  ["class":protected]=>
  string(8) "MyWorker"
  ["workers":protected]=>
  NULL
  ["work":protected]=>
  NULL
  ["ctor":protected]=>
  array(1) {
    [0]=>
    object(Something)#2 (0) {
    }
  }
  ["last":protected]=>
  int(0)
}

add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top