mirror of
https://github.com/danog/file.git
synced 2024-11-29 20:09:10 +01:00
Update to amphp/parallel 0.2
This commit is contained in:
parent
f4c5a623ff
commit
fb58fe8dd5
@ -34,7 +34,7 @@
|
||||
"require": {
|
||||
"amphp/amp": "^2",
|
||||
"amphp/byte-stream": "^1",
|
||||
"amphp/parallel": "^0.1.8"
|
||||
"amphp/parallel": "^0.2"
|
||||
},
|
||||
"require-dev": {
|
||||
"amphp/phpunit-util": "^1",
|
||||
|
@ -15,7 +15,7 @@ use Amp\Parallel\Worker\Task;
|
||||
* @internal
|
||||
*/
|
||||
class FileTask implements Task {
|
||||
const ENV_PREFIX = self::class . '#';
|
||||
const ENV_PREFIX = "amp/file#";
|
||||
|
||||
/** @var string */
|
||||
private $operation;
|
||||
@ -112,8 +112,8 @@ class FileTask implements Task {
|
||||
return ([$file, \substr($this->operation, 1)])(...$this->args);
|
||||
|
||||
case "fclose":
|
||||
$environment->delete($id);
|
||||
$file->close();
|
||||
$environment->delete($this->id);
|
||||
return;
|
||||
|
||||
default:
|
||||
|
@ -3,7 +3,6 @@
|
||||
namespace Amp\File;
|
||||
|
||||
use Amp\Coroutine;
|
||||
use Amp\Deferred;
|
||||
use Amp\Parallel\Worker;
|
||||
use Amp\Parallel\Worker\Pool;
|
||||
use Amp\Parallel\Worker\TaskException;
|
||||
@ -23,42 +22,29 @@ class ParallelDriver implements Driver {
|
||||
*/
|
||||
public function __construct(Pool $pool = null) {
|
||||
$this->pool = $pool ?: Worker\pool();
|
||||
if (!$this->pool->isRunning()) {
|
||||
$this->pool->start();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function open(string $path, string $mode): Promise {
|
||||
$worker = $this->pool->get();
|
||||
|
||||
$task = new Internal\FileTask("fopen", [$path, $mode]);
|
||||
|
||||
$deferred = new Deferred;
|
||||
$promise = $worker->enqueue($task);
|
||||
$promise->onResolve(static function ($exception, array $result = null) use ($worker, $deferred, $path) {
|
||||
if ($exception) {
|
||||
$deferred->fail($exception);
|
||||
return;
|
||||
return call(function () use ($path, $mode) {
|
||||
$worker = $this->pool->get();
|
||||
try {
|
||||
list($id, $size, $mode) = yield $worker->enqueue(new Internal\FileTask("fopen", [$path, $mode]));
|
||||
} catch (TaskException $exception) {
|
||||
throw new FilesystemException("Could not open file", $exception);
|
||||
} catch (WorkerException $exception) {
|
||||
throw new FilesystemException("Could not send open request to worker", $exception);
|
||||
}
|
||||
|
||||
list($id, $size, $mode) = $result;
|
||||
|
||||
$deferred->resolve(new ParallelHandle($worker, $id, $path, $size, $mode));
|
||||
return new ParallelHandle($worker, $id, $path, $size, $mode);
|
||||
});
|
||||
|
||||
return $deferred->promise();
|
||||
}
|
||||
|
||||
private function runFileTask(Internal\FileTask $task): \Generator {
|
||||
try {
|
||||
return yield $this->pool->enqueue($task);
|
||||
} catch (TaskException $exception) {
|
||||
if (\strcasecmp(\substr($exception->getName(), -5), "Error") === 0) {
|
||||
throw new \Error($exception->getMessage());
|
||||
}
|
||||
throw new FilesystemException("The file operation failed", $exception);
|
||||
} catch (WorkerException $exception) {
|
||||
throw new FilesystemException("Could not send the file task to worker", $exception);
|
||||
|
@ -47,14 +47,6 @@ function driver(): Driver {
|
||||
return new EioDriver;
|
||||
}
|
||||
|
||||
if (\strncasecmp(\PHP_OS, "WIN", 3) === 0) {
|
||||
return new BlockingDriver;
|
||||
}
|
||||
|
||||
if (\PHP_SAPI !== "cli" && \PHP_SAPI !== "phpdbg") { // We don't have a binary to launch sub-processes
|
||||
return new BlockingDriver;
|
||||
}
|
||||
|
||||
if (\defined("AMP_WORKER")) { // Prevent spawning infinite workers.
|
||||
return new BlockingDriver;
|
||||
}
|
||||
|
@ -11,7 +11,6 @@ class ParallelDriverTest extends DriverTest {
|
||||
protected function execute(callable $cb) {
|
||||
Loop::run(function () use ($cb) {
|
||||
$pool = new DefaultPool;
|
||||
$pool->start();
|
||||
|
||||
File\filesystem(new File\ParallelDriver($pool));
|
||||
yield call($cb);
|
||||
|
@ -11,7 +11,6 @@ class ParallelHandleTest extends AsyncHandleTest {
|
||||
protected function execute(callable $cb) {
|
||||
Loop::run(function () use ($cb) {
|
||||
$pool = new DefaultPool;
|
||||
$pool->start();
|
||||
|
||||
File\filesystem(new File\ParallelDriver($pool));
|
||||
yield call($cb);
|
||||
|
Loading…
Reference in New Issue
Block a user