Pool :: collect如何工作? [英] How does Pool::collect works?

查看:168
本文介绍了Pool :: collect如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

帮助我了解Pool :: collect的工作原理。

Help me understand how exactly Pool::collect works.

池:: collect - 收集已完成任务的参考

Pool::collect — Collect references to completed tasks

public void Pool::collect ( Callable $collector )

$我假设是: Pool :: collect 注册一个函数,它将在每个 \ Threaded $ task 完成。所以,我做了:

What I assume was: Pool::collect registers a function, which will be called after each \Threaded $task is completed. So, I did:

<?php
$pool = new Pool(4);
$pool->collect($collector);
$pool->submit(new Task);

无法使用。但以下操作:

Didn't work. But the following does:

<?php
$pool = new Pool(4);
$pool->submit(new Task);
$pool->collect($collector);

所以,我猜想 Pool :: collect 是:将 $ collector 附加到先前提交的每个 \Threaded $ task

So, I guess what Pool::collect does is: attaches the $collector to each \Threaded $task previously submitted.

现在,当调用 $ collector 时?我假设在 Threaded :: run()完成后调用。再次错误。

Now, when exactly the $collector is called? I assume was called after Threaded::run() is completed. Wrong again.

<?php
class Task extends Threaded {
    public function run () { echo "Task complete\n"; }
}

$collector = function (\Task $task) {
    echo "Collect task\n";
    return true;
};

$pool = new Pool(4);
$pool->submit(new Task);
$pool->collect($collector);
$pool->shutdown();

输出:

Collect task
Task complete

$ collector

文档没有多说。没有事件说, $ collector 必须返回一个布尔值。 我不知道

The documentation doesn't say much. Doesn't event say that the $collector must return a boolean value. I didn't know that.

我试图使用Pool :: collect作为一种回调在每个$任务完成后。

编辑1.

Edit 1. What about this attempt?


推荐答案

c $ c>遍历将每个对象传递给 $ collector 的对象列表。

<$ c $当引擎可以销毁对 Threaded $ collector 函数应返回 true >对象。

The ::collect functionality was moved to Worker, though it's still exposed by Pool for utility.

:: collect 功能已移到 Worker ,尽管它仍然由 >

有两个列表,一个准备执行的项目列表,以及已执行(或正在执行)的另一个项目。

There are two lists, one list of items ready to be executed, and another of items that have been executed (or is currently being executed).

Pool :: collect 遍历第二个列表,即已执行(或正在执行)的项目。

Pool::collect traverses the second list, items that have been executed (or is currently being executed).

Pool :: collect 返回所有 Worker Pool 中的对象以帮助安排垃圾收集。

Pool::collect returns the number of items still left in the garbage list for all Worker objects in the Pool to aid with scheduling garbage collection.

<?php
$pool = new Pool(4);

while (@$i++<10) {
    $pool->submit(new class($i) extends Collectable {
        public function __construct($id) {
            $this->id = $id;
        }

        public function run() {
            printf(
                "Hello World from %d\n", $this->id);
            $this->setGarbage();
        }

        public $id;
    });
}

while ($pool->collect(function(Collectable $work){
    printf(
        "Collecting %d\n", $work->id);
    return $work->isGarbage();
})) continue;

$pool->shutdown();
?>

会产生类似的结果:

Hello World from 1
Hello World from 2
Hello World from 3
Hello World from 4
Hello World from 8
Collecting 1
Hello World from 7
Hello World from 5
Collecting 5
Hello World from 6
Collecting 9
Collecting 2
Collecting 6
Collecting 10
Hello World from 9
Collecting 3
Collecting 7
Collecting 4
Hello World from 10
Collecting 8
Collecting 5
Collecting 9
Collecting 10

注意 9 出现两次;第一次它是当前正在执行的对象,因此它再次收集。

Notice that Collecting 9 appears twice; The first time it is the object currently being executed, and so it comes up for collection again.

这篇关于Pool :: collect如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆