private function dequeue() { $this->isActive = true; $op = \array_shift($this->queue); switch ($op->type) { case self::OP_READ: \call_user_func($this->incrementor, 1); $this->isActive = true; \eio_read($this->fh, $op->readLen, $op->position, $priority = null, [$this, "onRead"], $op); break; case self::OP_WRITE: \call_user_func($this->incrementor, 1); $this->isActive = true; \eio_write($this->fh, $op->writeData, \strlen($op->writeData), $op->position, $priority = null, [$this, "onWrite"], $op); break; } }
public function test_readfile_eio() { $time_start = microtime(true); echo "reading file is started....<br/>"; $file_open_cb = function ($data, $result) { eio_read($result, 10485760, 0, EIO_PRI_DEFAULT, function ($data, $result) { echo str_word_count($result) . "<br/>"; eio_close($data); }, $result); }; $filename = './assets/data/access.log'; eio_open($filename, EIO_O_RDWR, NULL, EIO_PRI_DEFAULT, $file_open_cb, $filename); $filename = './assets/data/error.log'; eio_open($filename, EIO_O_RDWR, NULL, EIO_PRI_DEFAULT, $file_open_cb, $filename); echo "reading file is finish....<br/>"; eio_event_loop(); $time_end = microtime(true); $time = $time_end - $time_start; echo "Execution time: {$time} seconds<br/>"; }
private function onGetFstat($fhAndPromisor, $result, $req) { list($fh, $promisor) = $fhAndPromisor; if ($result === -1) { $promisor->fail(new \RuntimeException(\eio_get_last_error($req))); return; } $offset = 0; $length = $result["size"]; $priority = \EIO_PRI_DEFAULT; \eio_read($fh, $length, $offset, $priority, [$this, "onGetRead"], $fhAndPromisor); }
/** * Reads file chunk-by-chunk * @param callable $cb Callback * @param callable $chunkcb Callback of chunk * @param integer $pri Priority * @return resource|false */ public function readAllChunked($cb = null, $chunkcb = null, $pri = EIO_PRI_DEFAULT) { $cb = CallbackWrapper::forceWrap($cb); if (!$this->fd) { if ($cb) { $cb($this, false); } return false; } return $this->statRefresh(function ($file, $stat) use($cb, $chunkcb, $pri) { if (!$stat) { $cb($file, false); return; } $offset = 0; $size = $stat['size']; eio_read($file->fd, min($file->chunkSize, $size), $offset, $pri, $this->readAllChunkedGenHandler($cb, $chunkcb, $size, $offset, $pri), $file); }, $pri); }
/** * {@inheritdoc} */ public function read(int $length = 0, string $byte = null, float $timeout = 0) : \Generator { if (!$this->isReadable()) { throw new UnreadableException('The file is no longer readable.'); } if (0 > $length) { throw new InvalidArgumentError('The length must be a non-negative integer.'); } if (0 === $length) { $length = self::CHUNK_SIZE; } $remaining = $this->size - $this->position; $length = $length > $remaining ? $remaining : $length; $delayed = new Delayed(); \eio_read($this->handle, $length, $this->position, null, function (Delayed $delayed, $result, $req) { if (-1 === $result) { $delayed->reject(new FileException(sprintf('Reading from file failed: %s.', \eio_get_last_error($req)))); } else { $delayed->resolve($result); } }, $delayed); $this->poll->listen(); if ($timeout) { $delayed = $delayed->timeout($timeout); } try { $data = (yield $delayed); } finally { $this->poll->done(); } $byte = strlen($byte) ? $byte[0] : null; if (null !== $byte && false !== ($position = strpos($data, $byte))) { $data = substr($data, 0, $position + 1); } $this->position += strlen($data); return $data; }
public function readAllChunked($cb = null, $chunkcb = null, $pri = EIO_PRI_DEFAULT) { $this->stat(function ($file, $stat) use($cb, $chunkcb, $pri) { if (!$stat) { call_user_func($cb, $file, false); return; } $offset = 0; $size = $stat['size']; $handler = function ($file, $data) use($cb, $chunkcb, &$handler, $size, &$offset, $pri) { call_user_func($chunkcb, $file, $data); $offset += strlen($data); $len = min($file->chunkSize, $size - $offset); if ($offset >= $size) { call_user_func($cb, $file, true); return; } eio_read($file->fd, $len, $offset, $pri, $handler, $file); }; eio_read($file->fd, min($file->chunkSize, $size), $offset, $pri, $handler, $file); }, $pri); }