mirror of
https://github.com/danog/parallel.git
synced 2025-01-22 22:11:11 +01:00
55 lines
1.4 KiB
PHP
55 lines
1.4 KiB
PHP
#!/usr/bin/env php
|
|
<?php
|
|
|
|
use Amp\Parallel\Sync;
|
|
use Amp\Parallel\Worker;
|
|
|
|
// Doesn't exist in phpdbg...
|
|
if (\function_exists('cli_set_process_title')) {
|
|
@cli_set_process_title('amp-worker');
|
|
}
|
|
|
|
// Redirect all output written using echo, print, printf, etc. to STDERR.
|
|
ob_start(function ($data) {
|
|
fwrite(STDERR, $data);
|
|
return '';
|
|
}, 1, PHP_OUTPUT_HANDLER_CLEANABLE | PHP_OUTPUT_HANDLER_FLUSHABLE);
|
|
|
|
(function () {
|
|
$paths = [
|
|
dirname(__DIR__, 3) . '/autoload.php',
|
|
dirname(__DIR__) . '/vendor/autoload.php',
|
|
];
|
|
|
|
$autoloadPath = null;
|
|
foreach ($paths as $path) {
|
|
if (file_exists($path)) {
|
|
$autoloadPath = $path;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if ($autoloadPath === null) {
|
|
fwrite(STDERR, 'Could not locate autoload.php.');
|
|
exit(1);
|
|
}
|
|
|
|
require $autoloadPath;
|
|
})();
|
|
|
|
Amp\Loop::run(function () {
|
|
$channel = new Sync\ChannelledSocket(STDIN, STDOUT);
|
|
$environment = new Worker\BasicEnvironment;
|
|
$runner = new Worker\Internal\TaskRunner($channel, $environment);
|
|
|
|
try {
|
|
$result = new Sync\Internal\ExitSuccess(yield $runner->run());
|
|
} catch (Sync\ChannelException $exception) {
|
|
return; // Parent context died, simply exit.
|
|
} catch (Throwable $exception) {
|
|
$result = new Sync\Internal\ExitFailure($exception);
|
|
}
|
|
|
|
$channel->send($result); // Do not yield sending result on channel, process does not care if result arrives.
|
|
});
|