4. Hello World
use AnyEvent;
my $cv = AnyEvent->condvar;
my $w = AnyEvent->timer(
'after' => 5,
'cb' => sub {
print "hello worldn";
$cv->send;
}
);
$cv->recv;
5. watcher
I/O
timer
signal
child process
idle
condvar
AnyEvent::Handle
AnyEvent::DNS
AnyEvent::Socket
6. I/O Watcher
use AnyEvent;
$| = 1; print "enter your name> ";
my $name;
my $wait_for_input = AnyEvent->io (
fh => *STDIN, # which file handle to check
poll => "r", # which event to wait for ("r"ead data)
cb => sub { # what callback to execute
$name = <STDIN>; # read it
}
);
# do something else here
7. Timer Watcher
my $once_per_second = AnyEvent->timer (
after => 0, # first invoke ASAP
interval => 1, # then invoke every second
cb => sub { # the callback to invoke
print "hin";
},
);
9. Watcher
use AnyEvent;
sub aaa {
my $w = AnyEvent->Timer(
after => 3,
interval => 5,
cb => sub { print "hellon"; }
);
}
my $cv = AnyEvent->condvar;
aaa();
$cv->recv; # won't print hello every 5 seconds.
10. Watcher
use AnyEvent;
my $w; # Watcher
sub aaa {
$w = AnyEvent->Timer(
after => 3,
interval => 5,
cb => sub { print "hellon"; }
);
}
my $cv = AnyEvent->condvar;
aaa();
$cv->recv; # will print hello every 5 seconds.
11. Watcher
use AnyEvent;
sub aaa {
# watcher
my $w; $w = AnyEvent->Timer(
after => 3,
interval => 5,
cb => sub { print "hellon"; $w; }
);
}
my $cv = AnyEvent->condvar;
aaa();
$cv->recv; # will print hello every 5 seconds.
12. Watcher
use AnyEvent;
sub aaa {
my $w; $w = AnyEvent->timer(
after => 5,
cb => sub {
print "hellon";
undef $w;
});
}
aaa();
AnyEvent->condvar()->recv();
13. CondVar
main loop POE::Kernal->run();
$thread->join();
“This module is an AnyEvent user, you
need to make sure that you use and
run a supported event loop.”
14. CondVar
use AnyEvent;
my $w = AnyEvent->timer(
after => 0,
interval => 5,
cb => sub {
print "hellon";
}
);
my $cv = AnyEvent->condvar;
$cv->recv; # just like while (1) {...}
15. CondVar
use AnyEvent;
use AnyEvent::HTTP;
# ...
my @cvs;
for (1 .. 5) {
my $cv = AnyEvent->condvar; # like thread id
push @cvs, $cv;
http_get("http://some_url", sub {
$cv->send; # like return in thread func
});
}
$_->recv for @cvs; # like $_->join for @threads;
# ...
17. use AnyEvent;
my $x = "Tom";
AnyEvent->timer(afer => 5, cb => sub {
print "hello $x";
}
18. use AnyEvent;
use AnyEvent::Handle;
my $cv = AnyEvent->condvar;
my $hdl; $hdl = new AnyEvent::Handle
fh => *STDIN,
on_error => sub {
my ($hdl, $fatal, $msg) = @_;
warn "got error $msgn";
$hdl->destroy;
$cv->send;
};
# send some request line
$hdl->push_write ("getinfo015012");
# read the response line
$hdl->push_read (line => sub {
my ($hdl, $line) = @_;
warn "got line <$line>n";
$cv->send;
});
$cv->recv;
19. use AnyEvent;
use AnyEvent::HTTP;
my $cv = AnyEvent->condvar;
http_get("http://www.sina.com.cn", # watcher
sub {
my ($data, $headers) = @_;
print $headers->{Status}, "n";
print $data, "n";
$cv->send;
});
$cv->recv;
20. use AnyEvent; # not AE
# file handle or descriptor readable
my $w = AE::io $fh, 0, sub { ... };
# one-shot or repeating timers
my $w = AE::timer $seconds, 0, sub { ... }; # once
my $w = AE::timer $seconds, $interval, sub { ... }; # repeated
print AE::now; # prints current event loop time
print AE::time; # think Time::HiRes::time or simply
CORE::time.
my $cv = AE::cv;
21.
22. http_get_retry("http://www.sina.com.cn",
max_retries => 3,
sub {
my ($data, $headers) = @_;
if (defined $data) {
print "load ok";
} else {
print "try 3 times failed";
}
});