Your SlideShare is downloading. ×
0
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 ...
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, #...
Timer Watcher     my $once_per_second = AnyEvent->timer (      after => 0,    # first invoke ASAP      interval => 1, # th...
WatcherWatcher
Watcheruse AnyEvent;sub aaa {    my $w = AnyEvent->Timer(         after => 3,         interval => 5,         cb => sub { p...
Watcheruse AnyEvent;my $w;      #   Watchersub aaa {    $w = AnyEvent->Timer(         after => 3,         interval => 5,  ...
Watcheruse AnyEvent;sub aaa {    #           watcher    my $w; $w = AnyEvent->Timer(         after => 3,         interval ...
Watcheruse AnyEvent;sub aaa {    my $w; $w = AnyEvent->timer(         after => 5,         cb => sub {               print ...
CondVar   main loop       POE::Kernal->run();               $thread->join();“This module is an AnyEvent user, youneed to m...
CondVaruse AnyEvent;my $w = AnyEvent->timer(     after => 0,     interval => 5,     cb => sub {         print "hellon";   ...
CondVaruse AnyEvent;use AnyEvent::HTTP;#                 ...my @cvs;for (1 .. 5) {    my $cv = AnyEvent->condvar;         ...
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_err...
use AnyEvent;use AnyEvent::HTTP;my $cv = AnyEvent->condvar;http_get("http://www.sina.com.cn",   #      watcher        sub ...
use AnyEvent; # not AE  # file handle or descriptor readable  my $w = AE::io $fh, 0, sub { ... };  # one-shot or repeating...
http_get_retry("http://www.sina.com.cn",    max_retries => 3,    sub {          my ($data, $headers) = @_;          if (de...
http_get_file("http://aaa.com/somefile.tar.gz",    "/data/files/somefile.tar.gz",    max_concurrent => 20,    sub {       ...
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?
Any event intro
Upcoming SlideShare
Loading in...5
×

Any event intro

2,147

Published on

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

Published in: Technology, Sports, Business
1 Comment
2 Likes
Statistics
Notes
  • nice。
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total Views
2,147
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
29
Comments
1
Likes
2
Embeds 0
No embeds

No notes for slide

Transcript of "Any event intro"

  1. 1. AnyEvent Achilles Xuformalin14@gmail.com
  2. 2. ContentsHello WorldWatcherCondVarBugs
  3. 3. EV, libevent, POE, Glib, QT, Tk
  4. 4. Hello Worlduse AnyEvent;my $cv = AnyEvent->condvar;my $w = AnyEvent->timer( after => 5, cb => sub { print "hello worldn"; $cv->send; });$cv->recv;
  5. 5. watcher I/O timer signal child process idlecondvar AnyEvent::Handle AnyEvent::DNS AnyEvent::Socket
  6. 6. 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
  7. 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"; },);
  8. 8. WatcherWatcher
  9. 9. 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.
  10. 10. 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.
  11. 11. 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.
  12. 12. Watcheruse AnyEvent;sub aaa { my $w; $w = AnyEvent->timer( after => 5, cb => sub { print "hellon"; undef $w; });}aaa();AnyEvent->condvar()->recv();
  13. 13. 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.”
  14. 14. CondVaruse AnyEvent;my $w = AnyEvent->timer( after => 0, interval => 5, cb => sub { print "hellon"; });my $cv = AnyEvent->condvar;$cv->recv; # just like while (1) {...}
  15. 15. 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;# ...
  16. 16. closurePP Par::Packerperl -T
  17. 17. use AnyEvent;my $x = "Tom";AnyEvent->timer(afer => 5, cb => sub { print "hello $x";}
  18. 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. 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. 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 simplyCORE::time. my $cv = AE::cv;
  21. 21. 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"; } });
  22. 22. 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}; } });
  23. 23. http_get( $url, sub { ... }); DNS Resolving TCP Connecting Sending Request Reading Response
  24. 24. Bugs socketaccept push_readrtimeout EV PurePerl
  25. 25. Bugs Solution timer*AE::now = sub { return AE::time; }*AE::now = *AE::time doesnt work
  26. 26. Questions?
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×