Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed timer lifespan in 2013 advent cal async example #18

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions 1to4-Nonblocking-Streaming/bin/five-times-evented.psgi
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@ use AnyEvent;
use warnings;
use strict;

my $watcher;
my $timer_model = sub {
my $writer = shift;
my $count = 1;
$watcher = AnyEvent->timer(
my $timer;
$timer = AnyEvent->timer(
after => 0,
interval => 1,
cb => sub {
$writer->write(scalar localtime ."\n");
if(++$count > 5) {
$writer->close;
undef $watcher;
# this cancels the timer, and breaks a circular reference
undef $timer;
}
});
};
Expand All @@ -26,6 +27,7 @@ my $psgi_app = sub {
[200, [ 'Content-Type' => 'text/plain' ]]);

$timer_model->($writer);

# our timer lives on via a circular reference.
# return value is ignored
};
};