mirror of
https://github.com/danog/amp.git
synced 2024-11-30 04:29:08 +01:00
Add garbage collection example with iterators
This commit is contained in:
parent
b873f8bd71
commit
46b1a27825
34
examples/iterators/emitter-gc.php
Normal file
34
examples/iterators/emitter-gc.php
Normal file
@ -0,0 +1,34 @@
|
||||
#!/usr/bin/env php
|
||||
<?php
|
||||
|
||||
require __DIR__ . '/../../vendor/autoload.php';
|
||||
|
||||
use Amp\Delayed;
|
||||
use Amp\Emitter;
|
||||
use Amp\Loop;
|
||||
|
||||
Loop::run(function () {
|
||||
$emitter = new Emitter;
|
||||
|
||||
Loop::defer(function () use ($emitter) {
|
||||
try {
|
||||
yield $emitter->emit(1);
|
||||
print "Emit done.\n";
|
||||
yield $emitter->emit(2);
|
||||
print "Never reached...\n";
|
||||
$emitter->complete();
|
||||
} finally {
|
||||
print "Garbage collected...\n";
|
||||
}
|
||||
});
|
||||
|
||||
$iterator = $emitter->iterate();
|
||||
yield $iterator->advance();
|
||||
yield $iterator->advance();
|
||||
yield new Amp\Delayed(0);
|
||||
|
||||
unset($emitter, $iterator);
|
||||
gc_collect_cycles();
|
||||
|
||||
print "Done.\n";
|
||||
});
|
Loading…
Reference in New Issue
Block a user