2017 © Pedro Peláez
 

library leak-safe-generator

Generator wrapper to prevent memory leaks

image

jced-artem/leak-safe-generator

Generator wrapper to prevent memory leaks

  • Tuesday, December 13, 2016
  • by jced_artem
  • Repository
  • 1 Watchers
  • 0 Stars
  • 6 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

Leak safe PHP generator wrapper

Install

composer require jced-artem/leak-safe-generator, (*1)

If you want to prevent memory leaks when you use generators with handlers and you bored to use try..finally constructions - this class could help., (*2)

Example

function parsePages() {
    $ch = curl_init();
    // ... some options
    while ($condition) {
        yield curl_exec($ch);
    }
    curl_close($ch);
}

foreach (parsePages() as $page) {
    if ($someCondition) {
        break;
    }
    // ...
}

Problem

If you break loop before get all yields you will have not closed handler $ch and this can cause memory leaks., (*3)

Solution

function parsePages() {
    $ch = curl_init();
    // ... some options
    try {
        $finished = false;
        while ($condition) {
            yield curl_exec($ch);
        }
        $finished = true;
    } finally {
        // close anyway
        curl_close($ch);
        if ($finished) {
            // do something if reached last element
        } else {
            // do something on break
        }
    }
}

New problem

This code isn't usable if you make several generators, (*4)

New solution

$lsg = new LeakSafeGenerator();
$lsg
    ->init(function() {
        $this->ch = curl_init();
        // ... some options
        while ($condition) {
            yield curl_exec($this->ch);
        }
    })
    ->onInterrupt(function() {
        // do something on break
    })
    ->onComplete(function() {
        // do something if reached last element
    })
    ->onFinish(function() {
        curl_close($this->ch);
    })
;
foreach ($lsg->getGenerator() as $page) {
    if ($someCondition) {
        break;
    }
    // ...
}

or shorter version, (*5)

$lsg = new LeakSafeGenerator(
    function() {
        $this->ch = curl_init();
        // ... some options
        while ($condition) {
            yield curl_exec($this->ch);
        }
    },
    function() {
        curl_close($this->ch);
    }
);

And you don't need to create additional functions/methods and write try..finally constructions for each generator., (*6)

The Versions

13/12 2016

dev-master

9999999-dev https://github.com/jced-artem

Generator wrapper to prevent memory leaks

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

  • php >=5.5.0

by Artem Jced

php generator wrapper memory leak

13/12 2016

1.0.0

1.0.0.0 https://github.com/jced-artem

Generator wrapper to prevent memory leaks

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

  • php >=5.5.0

by Artem Jced

php generator wrapper memory leak