Help me understand how exactly Pool::collect works.
Pool::collect — Collect references to completed tasks
public void Pool::collect ( Callable $collector )
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);
So, I guess what Pool::collect
does is: attaches the $collector
to each \Threaded $task
previously submitted.
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();
Outputs:
Collect task
Task complete
$collector
is called before Threaded::run()
is completed.
The documentation doesn't say much. Doesn't event say that the $collector
must return a boolean value. I didn't know that.
I was trying to use Pool::collect as kind of a callback after each $task is completed. I think I'm in the wrong path.
Edit 1. What about this attempt?