5

Process forks - Piece of Code - stitcher.io

 3 years ago
source link: https://www.stitcher.io/blog/object-oriented-generators
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
Process forks
By continuing your visit to this site, you accept the use of cookies. Read more.

Scout APM helps PHP developers pinpoint N+1 queries, memory leaks & more so you can troubleshoot fast & get back to coding faster. Start your free 14-day trial today.

« back — written by Brent on June 17, 2017

Object oriented generators

The following code shows an object oriented way of implementing a well known generator function: to read lines from a large file.

class FileReader implements \Iterator 
{
    private $handle;
    private $current;

    public static function read(string $fileName) : FileReader {
        return new self($fileName);
    }

    public function __construct(string $fileName) {
        $this->handle = fopen($fileName, 'r');
        $this->next();
    }

    public function __destruct() {
        fclose($this->handle);
    }

    public function current() {
        return $this->current;
    }

    public function next() {
        $this->current = fgets($this->handle);
    }

    public function key() {
        return ftell($this->handle);
    }

    public function valid() {
        return !feof($this->handle);
    }

    public function rewind() {
        rewind($this->handle);
    }
}

Using the file reader.

$lines = FileReader::read('path_to_large_file.txt');

foreach ($lines as $line) {
    echo $line;
}

A comparison to using generators and the yield keyword, based on the tests I ran:

  • This approach takes the same amount of time to execute.
  • It has the same memory footprint as a generator function.
  • It has the benefit of easier re-usability (in my opinion).

In comparison to file_get_contents: reading the same file required of 15MB of memory, whilst this solution required only 2MB, because it only reads one line in memory at a time.

To round up, this is the generator solution using yield.

function read($fileName) {
    $handle = fopen($fileName, 'r');

    while (!feof($handle)) {
        yield fgets($handle);
    }

    fclose($handle);
}

$lines = read('path_to_large_file');

foreach ($lines as $line) {
    echo $line;
}

Next up:  Process forks

Follow me: TwitterRSSNewsletterPatreonGitHub

© 2020 stitcher.io — Cookies & Privacy


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK