7. AnyEvent
• framework for event driven programming
• thin layer above several event loops
• EV, Event, Gtk, Tk, POE
• deals with IO handles (incl. unix domain
sockets), timers, signals, processes
• thousands of sockets with proper event
loop (EV)
8. Watchers and
Condvars
• AnyEvent has two basic concepts
• Watchers
• Condvars
• Watchers wait for externals events: socket
input, timer expiration, child process exits
• Condvars are a synchronization primitive
9. IO Watchers
• Covers file handles, sockets, unix domain
sockets
• Deals with buffered reads and writes
• Can also deal with “reading lines” or “block
of bytes”: callback only when we have
everything
11. Condvars
• Provide syncronization inside your program
• A simple broadcast service
• Multiple interested parties
• Anybody can trigger it
• All the interested parties will be notified
12. Read a line
$| = 1; print quot;enter your name> quot;;
my $name = <STDIN>;
13. Read a line, take II
use AnyEvent;
$| = 1; print quot;enter your name> quot;;
my $name;
my $wait_for_input = AnyEvent->io (
fh => *STDIN,
poll => quot;rquot;,
cb => sub {
$name = <STDIN>;
}
);
14. Demo
• A simple broadcast server
• Multiple clients
• Broadcasts lines sent by one client to all
the others
• Code http://github.com/melo/presentations/