(PECL pthreads >= 3.0.0)
Worker::collect — 收集对已完成任务的引用
collector
一个 Callable 收集器,它返回一个布尔值,表示任务是否可以被收集。只有在极少数情况下才需要使用自定义收集器。
工作线程堆栈上剩余待收集任务的数量。
示例 #1 Worker::collect() 的基本示例
<?php
$worker = new Worker();
echo "堆栈上目前有 {$worker->collect()} 个任务需要收集\n";
for ($i = 0; $i < 15; ++$i) {
$worker->stack(new class extends Threaded {});
}
echo "堆栈上剩余 {$worker->collect()} 个任务需要收集\n";
$worker->start();
while ($worker->collect()); // 阻塞直到所有任务执行完毕
echo "现在堆栈上剩余 {$worker->collect()} 个任务需要收集\n";
$worker->shutdown();
以上示例将输出
There are currently 0 tasks on the stack to be collected There are 15 tasks remaining on the stack to be collected There are now 0 tasks on the stack to be collected