Any event intro
Upcoming SlideShare
Loading in...5
×
 

Any event intro

on

  • 2,135 views

From Beijing Perl Workshop 2011 http://conference.perlchina.org/bjpw2011/

From Beijing Perl Workshop 2011 http://conference.perlchina.org/bjpw2011/

Statistics

Views

Total Views
2,135
Views on SlideShare
2,134
Embed Views
1

Actions

Likes
2
Downloads
28
Comments
1

1 Embed 1

http://www.slideshare.net 1

Accessibility

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel

11 of 1

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
  • nice。
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Any event intro Any event intro Presentation Transcript

    • AnyEvent Achilles Xuformalin14@gmail.com
    • ContentsHello WorldWatcherCondVarBugs
    • EV, libevent, POE, Glib, QT, Tk
    • Hello Worlduse AnyEvent;my $cv = AnyEvent->condvar;my $w = AnyEvent->timer( after => 5, cb => sub { print "hello worldn"; $cv->send; });$cv->recv;
    • watcher I/O timer signal child process idlecondvar AnyEvent::Handle AnyEvent::DNS AnyEvent::Socket
    • I/O Watcheruse 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
    • 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"; },);
    • WatcherWatcher
    • Watcheruse AnyEvent;sub aaa { my $w = AnyEvent->Timer( after => 3, interval => 5, cb => sub { print "hellon"; } );}my $cv = AnyEvent->condvar;aaa();$cv->recv; # wont print hello every 5 seconds.
    • Watcheruse AnyEvent;my $w; # Watchersub 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.
    • Watcheruse 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.
    • Watcheruse AnyEvent;sub aaa { my $w; $w = AnyEvent->timer( after => 5, cb => sub { print "hellon"; undef $w; });}aaa();AnyEvent->condvar()->recv();
    • CondVar main loop POE::Kernal->run(); $thread->join();“This module is an AnyEvent user, youneed to make sure that you use andrun a supported event loop.”
    • CondVaruse AnyEvent;my $w = AnyEvent->timer( after => 0, interval => 5, cb => sub { print "hellon"; });my $cv = AnyEvent->condvar;$cv->recv; # just like while (1) {...}
    • CondVaruse 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;# ...
    • closurePP Par::Packerperl -T
    • use AnyEvent;my $x = "Tom";AnyEvent->timer(afer => 5, cb => sub { print "hello $x";}
    • 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;
    • 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;
    • 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 simplyCORE::time. my $cv = AE::cv;
    • 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"; } });
    • http_get_file("http://aaa.com/somefile.tar.gz", "/data/files/somefile.tar.gz", max_concurrent => 20, sub { my ($data, $headers) = @_; if (defined $data) { print "download ok"; } else { print "some error happens: " . $headers->{Status}; } });
    • http_get( $url, sub { ... }); DNS Resolving TCP Connecting Sending Request Reading Response
    • Bugs socketaccept push_readrtimeout EV PurePerl
    • Bugs Solution timer*AE::now = sub { return AE::time; }*AE::now = *AE::time doesnt work
    • Questions?