• Like
  • Save

Loading…

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

Asynchronous programming with AnyEvent

  • 15,327 views
Uploaded on

 

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
  • Good presentation !

    Thanks a lot for that
    Are you sure you want to
    Your message goes here
  • Slides 19-21 are misleading. They ignore the existence of POE::Loop::* modules, which are POE′s equivalent to AnyEvent::Impl::*. POE and its components have worked under other event loops since mid-2000.

    As of this writing, most POE components work seamlessly under 13 different event loops, according to search.cpan.org. And they should work under new event loops as people write POE::Loop modules to support their favorites. If anyone is interested in writing POE::Loop::IO_Async or POE::Loop::AnyEvent (or others), we′ll even write your tests. See POE::Test::Loops on CPAN. :)

    And see http://use.perl.org/~rcaputo/journal/39612 for a longer reply.
    Are you sure you want to
    Your message goes here
No Downloads

Views

Total Views
15,327
On Slideshare
0
From Embeds
0
Number of Embeds
2

Actions

Shares
Downloads
0
Comments
2
Likes
28

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. Asynchronous Event programming with AnyEvent Tatsuhiko Miyagawa YAPC::Asia 2009
  • 2. Questions: Are you familiar with Event loops?
  • 3. Questions: Have you used POE?
  • 4. Intro Basics Examples Tips
  • 5. AnyEvent Intro
  • 6. “DBI of event loops”
  • 7. (Used in Remedie!)
  • 8. Event loops on CPAN
  • 9. POE IO::Async Danga::Socket IO::Lambda Event Event::Lib EV Glib Qt Tk ...
  • 10. Incompatible APIs
  • 11. Different Styles
  • 12. #!/usr/bin/perl use POE; POE::Session->create( inline_states => { _start => sub { $_[HEAP]->{count} = 0; $_[KERNEL]->yield(‘incr’); }, incr => sub { warn $_[HEAP]->{count}++; $_[KERNEL]->yield(‘incr’); }, } ); POE::Kernel->run
  • 13. use IO::Async::Loop; use IO::Async::Stream; use Socket qw( SOCK_STREAM ); my $loop = IO::Async::Loop->new; $loop->connect( host => “example.com”, service => 80, socktype => SOCK_STREAM, on_connected => sub { my $socket = shift; ... } ); $loop->loop_forever;
  • 14. More important:
  • 15. CPAN Modules
  • 16. 250
  • 17. POE::Components on CPAN
  • 18. POE::Component::* don’t work with IO::Async etc.
  • 19. AnyEvent::Impl::*
  • 20. AnyEvent code can work under “Any Event” loops
  • 21. #!/usr/bin/perl use POE; use AnyEvent; POE::Session->create( inline_states => { ... } ); my $w = AnyEvent->timer( after => 0, cb => sub { ... } ); POE::Kernel->run; # Just works
  • 22. AnyEvent modules: 35
  • 23. CPAN authors: Stop writing PoCos :)
  • 24. Intro Basics Examples Tips
  • 25. #!/usr/bin/perl use AnyEvent; my $i; my $cv = AnyEvent->condvar; my $w = AnyEvent->timer( after => 0, interval => 1, cb => sub { warn “checking “, $i++; $cv->send() if $i > 10; } ); $cv->recv;
  • 26. Timer Watcher condvar I/O subprocess modules
  • 27. my $t1 = AnyEvent->timer( after => 0, cb => sub { # ... } ); my $t2 = AnyEvent->timer( after => 0, interval => 3, cb => sub { # ... } );
  • 28. my $cv = AnyEvent->condvar; my $w = AnyEvent->timer( after => 5, cb => sub { $cv->send(“DONE”); }, ); my $r = $cv->recv; # $r = ‘DONE’
  • 29. my $io = AnyEvent->io( fh => *STDIN, poll => ‘r’, cb => sub { # $fh is ready to read my $line = <$fh>; # scalar! } );
  • 30. use AnyEvent::Handle; my $hd; $hd = AnyEvent::Handle->new( fh => $fh, ); $hd->push_read(line => sub { my($hd, $line) = @_; }); $hd->push_read(chunk => 8, sub { my($hd, $bytes) = @_; }); $hd->push_read(json => sub { my($hd, $obj) = @_; });
  • 31. use AnyEvent::Socket; tcp_connect $host, $port, sub { my $fh = shift or die “FAIL”; my $hd = AnyEvent::Handle->new( fh => $fh, ); $hd->push_read(json => sub { my($hd2, $obj) = @_; }); };
  • 32. use AnyEvent::HTTP; http_get “http://google.com/”, sub { my($body, $header) = @_; # $header->{Status}; # $header->{‘content-type’}; };
  • 33. Intro Basics Examples Tips
  • 34. AnyEvent::Twitter::Stream
  • 35. Twitter Streaming API aka Firehose
  • 36. use AnyEvent::Twitter::Stream; my $cv = AnyEvent->condvar; my $s = AnyEvent::Twitter::Stream->new( username => $user, password => $pass, method => ‘filter’, track => ‘#yapc,#yapcasia2009’, on_tweet => sub { my $tweet = shift; printf “%s:%sn”, $tweet->{user}{screen_name}, $tweet->{text}; }, on_eof => $cv, ); $cv->recv;
  • 37. AnyEvent::ReverseHTTP
  • 38. client becomes server server becomes client
  • 39. Polling HTTP GET with AnyEvent::HTTP
  • 40. use AnyEvent::ReverseHTTP; my $guard = reverse_http $hostname, $token, sub { my $req = shift; return “Hello World”; }; AnyEvent->condvar->recv;
  • 41. Intro Basics Examples Tips
  • 42. Condvars
  • 43. Transactions (multi signals)
  • 44. Parallel downloads with AnyEvent::HTTP
  • 45. use AnyEvent::HTTP; my $cv = AnyEvent->condvar; for my $i (1..3) { $cv->begin; http_get "http://search.cpan.org/ search?q=$i", sub { my $body = shift; warn length $body; $cv->end; }; } $cv->recv;
  • 46. Scope of watchers
  • 47. for (1..10) { my $w = AnyEvent->timer( after => 1, cb => sub { ... } ); } AnyEvent->condvar->recv;
  • 48. for (1..10) { my $w = AnyEvent->timer( after => 1, cb => sub { ... } ); # $w goes out of scope! } AnyEvent->condvar->recv;
  • 49. for (1..10) { my $w; $w = AnyEvent->timer( after => 1, cb => sub { ... undef $w; } ); } AnyEvent->condvar->recv;
  • 50. multiple condvars waiting
  • 51. You can’t call ->recv on multiple $cv
  • 52. USe $cv->cb() instead
  • 53. for (1..10) { my $cv = AnyEvent->condvar; my $w; $w = AnyEvent->timer( after => 1, cb => sub { $w; $cv->send }, ); $cv->recv; # blocks }
  • 54. for (1..10) { my $cv = AnyEvent->condvar; my $w; $w = AnyEvent->timer( after => 1, cb => sub { $w; $cv->send($v) }, ); $cv->cb(sub { my $v = $cv->recv; ... }); }
  • 55. Or: use Coro::AnyEvent
  • 56. CPAN authors:
  • 57. Write a simple callback API
  • 58. use AnyEvent::mDNS; my $cv = AnyEvent->condvar; # $cv can be called as a callbak my $g = AnyEvent::mDNS::discover ‘_daap._tcp’, $cv; my @itunes = $cv->recv;
  • 59. Use guard { } to cleanup
  • 60. use AnyEvent::ReverseHTTP; { my $guard = reverse_http $h, $t, sub { my $req = shift; }; # ... }
  • 61. Beware of circular references (Scalar::Util::weaken)
  • 62. API to pass/return condvar for better control by users
  • 63. use AnyEvent::ReverseHTTP; my $guard = reverse_http $h, $t, sub { my $req = shift; my $cv = AnyEvent->condvar; my $t; $t = AnyEvent->timer( after => 5, cb => sub { my $res = HTTP::Response->new; undef $t; $cv->send($res); } }; return $cv; };
  • 64. New AE API
  • 65. use AnyEvent; my $t = AE::timer 0, 1, $cb; my $w = AE::io $fh, $write, $cb; my $cv = AE::cv;
  • 66. Summary
  • 67. AnyEvent Very useful Nice utilities No bloated POE stuff
  • 68. Questions?
  • 69. Thank you.