Bulgarian Perl Workshop 2010




    Event driven programing with 
             AnyEvent


                            Marian Marinov
                                 mm@yuhu.biz
                      
Event Driven programing

    ●   select() ..... and wait
    ●   fork() ..... and wait

    ●   poll/epoll – define..... call when ready
    ●   fork() + wait(WNOHANG)




                                   
AnyEvent supports

    ●   EV                  ●   Irssi
    ●   Event               ●   Qt
    ●   Glib                ●   Tk
    ●   Event::Lib          ●   Own pure perl loops
    ●   IO::Async
    ●   POE


                         
Log analyzer


    Blocking example:


        open FD, '-|', 'tail -f messages';
        while(<FD>) { xxxxx; }
        close FD;


                              
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;
   


                                         
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.



                                      
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



                                     
How it's done


    ●   Setup a condition
    ●   Write what you want to do (setup a watcher)
           –   setup a producer into the watcher
    ●   Set a stop point (consumer)




                                   

Anyevent

  • 1.
    Bulgarian Perl Workshop 2010 Event driven programing with  AnyEvent Marian Marinov mm@yuhu.biz    
  • 2.
    Event Driven programing ● select() ..... and wait ● fork() ..... and wait ● poll/epoll – define..... call when ready ● fork() + wait(WNOHANG)    
  • 3.
    AnyEvent supports ● EV ● Irssi ● Event ● Qt ● Glib ● Tk ● Event::Lib ● Own pure perl loops ● IO::Async ● POE    
  • 4.
    Log analyzer Blocking example: open FD, '-|', 'tail -f messages'; while(<FD>) { xxxxx; } close FD;    
  • 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.
    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.
    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.
    How it's done ● Setup a condition ● Write what you want to do (setup a watcher) – setup a producer into the watcher ● Set a stop point (consumer)