mirror of
https://github.com/danog/parallel.git
synced 2024-11-27 04:44:56 +01:00
Use Amp\call to run tasks
Handles generator to coroutine convertion and React promises automatically.
This commit is contained in:
parent
f0a034e871
commit
149dbe4ba3
@ -3,11 +3,10 @@
|
|||||||
namespace Amp\Parallel\Worker\Internal;
|
namespace Amp\Parallel\Worker\Internal;
|
||||||
|
|
||||||
use Amp\Coroutine;
|
use Amp\Coroutine;
|
||||||
use Amp\Failure;
|
|
||||||
use Amp\Parallel\Sync\Channel;
|
use Amp\Parallel\Sync\Channel;
|
||||||
use Amp\Parallel\Worker\Environment;
|
use Amp\Parallel\Worker\Environment;
|
||||||
use Amp\Promise;
|
use Amp\Promise;
|
||||||
use Amp\Success;
|
use function Amp\call;
|
||||||
|
|
||||||
class TaskRunner {
|
class TaskRunner {
|
||||||
/** @var \Amp\Parallel\Sync\Channel */
|
/** @var \Amp\Parallel\Sync\Channel */
|
||||||
@ -41,19 +40,7 @@ class TaskRunner {
|
|||||||
while ($job instanceof Job) {
|
while ($job instanceof Job) {
|
||||||
$task = $job->getTask();
|
$task = $job->getTask();
|
||||||
|
|
||||||
try {
|
$result = call([$task, 'run'], $this->environment);
|
||||||
$result = $task->run($this->environment);
|
|
||||||
|
|
||||||
if ($result instanceof \Generator) {
|
|
||||||
$result = new Coroutine($result);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!$result instanceof Promise) {
|
|
||||||
$result = new Success($result);
|
|
||||||
}
|
|
||||||
} catch (\Throwable $exception) {
|
|
||||||
$result = new Failure($exception);
|
|
||||||
}
|
|
||||||
|
|
||||||
$result->onResolve(function ($exception, $value) use ($job) {
|
$result->onResolve(function ($exception, $value) use ($job) {
|
||||||
if ($exception) {
|
if ($exception) {
|
||||||
|
Loading…
Reference in New Issue
Block a user