Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Anyevent

997 views

Published on

Bulgarian Perl Workshop 2010

Published in: Technology
  • Be the first to comment

Anyevent

  1. 1. Bulgarian Perl Workshop 2010 Event driven programing with  AnyEvent Marian Marinov mm@yuhu.biz   
  2. 2. Event Driven programing ● select() ..... and wait ● fork() ..... and wait ● poll/epoll – define..... call when ready ● fork() + wait(WNOHANG)   
  3. 3. AnyEvent supports ● EV ● Irssi ● Event ● Qt ● Glib ● Tk ● Event::Lib ● Own pure perl loops ● IO::Async ● POE   
  4. 4. Log analyzer Blocking example: open FD, -|, tail -f messages; while(<FD>) { xxxxx; } close FD;   
  5. 5. Log analyzer Non-blocking example: my $result_ready = AnyEvent->condvar;  my $w = AnyEvent­>io (  fh   => $fh,  poll => r,  cb   => sub { $result_ready­>send; }  );  $result_ready­>recv;       
  6. 6. AnyEvent methods ● AnyEvent­>io (fh => $fh, poll => "r", cb => sub { ...  }); ● AnyEvent­>timer (after => $seconds, cb => sub { ...  }); ● AnyEvent­>timer (after => $seconds, interval =>  $seconds, cb => ... ● AnyEvent­>now;  # prints current event loop time ● AnyEvent­>time; # think Time::HiRes::time or simply  CORE::time.   
  7. 7. AnyEvent methods ● AnyEvent­>signal (signal => "TERM", cb => sub { ... }); ● AnyEvent­>child (pid => $pid, cb => sub { ... }); ● AnyEvent­>idle (cb => sub { ... }); ● AnyEvent­>condvar; # condition state ● $condition­>send; # send signal to the receivers  ● $condition­>recv; # continue from here   
  8. 8. How its done ● Setup a condition ● Write what you want to do (setup a watcher) – setup a producer into the watcher ● Set a stop point (consumer)   

×