Your SlideShare is downloading. ×
0
Any event intro
Any event intro
Any event intro
Any event intro
Any event intro
Any event intro
Any event intro
Any event intro
Any event intro
Any event intro
Any event intro
Any event intro
Any event intro
Any event intro
Any event intro
Any event intro
Any event intro
Any event intro
Any event intro
Any event intro
Any event intro
Any event intro
Any event intro
Any event intro
Any event intro
Any event intro
Any event intro
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Any event intro

2,108

Published on

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

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,108
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
29
Comments
1
Likes
2
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. AnyEvent Achilles Xuformalin14@gmail.com
  • 2. ContentsHello WorldWatcherCondVarBugs
  • 3. EV, libevent, POE, Glib, QT, Tk
  • 4. Hello Worlduse 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 idlecondvar AnyEvent::Handle AnyEvent::DNS AnyEvent::Socket
  • 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. 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. WatcherWatcher
  • 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. 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. 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. Watcheruse 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, youneed to make sure that you use andrun a supported event loop.”
  • 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. 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. closurePP Par::Packerperl -T
  • 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 simplyCORE::time. my $cv = AE::cv;
  • 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. 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. http_get( $url, sub { ... }); DNS Resolving TCP Connecting Sending Request Reading Response
  • 24. Bugs socketaccept push_readrtimeout EV PurePerl
  • 25. Bugs Solution timer*AE::now = sub { return AE::time; }*AE::now = *AE::time doesnt work
  • 26. Questions?

×