1
0
mirror of https://github.com/danog/parallel.git synced 2024-12-11 16:49:51 +01:00
parallel/lib/Worker/WorkerThread.php
2018-10-26 18:29:45 -05:00

40 lines
1.2 KiB
PHP

<?php
namespace Amp\Parallel\Worker;
use Amp\Parallel\Context\Thread;
use Amp\Parallel\Sync\Channel;
use Amp\Promise;
/**
* A worker thread that executes task objects.
*/
final class WorkerThread extends TaskWorker
{
/**
* @param string $envClassName Name of class implementing \Amp\Parallel\Worker\Environment to instigate.
* Defaults to \Amp\Parallel\Worker\BasicEnvironment.
*/
public function __construct(string $envClassName = BasicEnvironment::class)
{
parent::__construct(new Thread(function (Channel $channel, string $className): Promise {
if (!\class_exists($className)) {
throw new \Error(\sprintf("Invalid environment class name '%s'", $className));
}
if (!\is_subclass_of($className, Environment::class)) {
throw new \Error(\sprintf("The class '%s' does not implement '%s'", $className, Environment::class));
}
$environment = new $className;
if (!\defined("AMP_WORKER")) {
\define("AMP_WORKER", "amp-worker");
}
$runner = new TaskRunner($channel, $environment);
return $runner->run();
}, $envClassName));
}
}