Welcome to the Monastery | |
PerlMonks |
AnyEvent for I/O eventsby elmex (Friar) |
on Apr 16, 2008 at 12:01 UTC ( [id://680777]=CUFP: print w/replies, xml ) | Need Help?? |
As POE seems to be very popular these days I wanted to share an alternative way of handling non-blocking I/O (and other events). For this I include a simple made up example, where I will show how to wait for a filehandle "readable" event and wait for a child process to finish. But first a quick overview/introduction to AnyEvent: AnyEvent provides us with 'watchers', which watch out for events. Events like a filehandle becoming readable or writable, a timer that reaches his timeout, a signal or a condition becoming true. To handle all these events AnyEvent needs an event loop. The interesting thing here is, that AnyEvent can use other event loops, like the one provided by Glib, which would allow easy integration with Gtk2 programs. Aside from that AnyEvent knows how to interface with the event loops of the Event and EV module, Tk and even Coro. For the case that none of these event loops is either loaded or their module installed on the system it comes with it's own pure Perl implementation. The major advantage comes in when you write a module and you need to wait for I/O events or need timers: you don't need to decide which event loop to use or even come up with your own select() implementation. If your module uses AnyEvent it can be used in POE, IO::Async, Gtk2, Tk, Coro, and other modules that also use AnyEvent. Now a simple example where I commented the most interesting parts:
Instead of calling the wait method there you could also start an event loop explicitly. For example if you would like to use the EV module you could use the loop and unloop functions instead of wait and broadcast:
Some links of the modules noticed:
Back to
Cool Uses for Perl
|
|