Hi!
I am pleased to announce the (very beta-ish) release of AnyEvent.
This module (which I started to write over a year ago after implementing
it's basic ideas in Net::FCP, but couldn't work on till recently)
implements a _very_ simple and stripped-down Event-like API.
The twist is that it can run on top of Event, Coro::Event, Glib and even Tk
(the latter with a lot of restricftions, but still).
It is mostly meant to be used by module authors who were reluctant to use
an event-based approach because they feared forcing users of their module
into a specific event loop. Often, this resulted in a blocking-only API
(witness all the blocking-only Net::*-modules).
AnyEvent provides very simple IO-watchers:
my $io_watcher = AnyEvent->io (fh => \*STDIN, poll => ’r’, cb => sub {
warn "io event <$_[0]>\n"; # will always output <r>
});
Very simple one-shot-timers:
my $timer = AnyEvent->timer (after => 1, cb => sub {
warn "timeout\n";
});
And "condition variables":
my $cv = AnyEvent->condvar;
$cv->wait; # wait till some handler calls ->broadcast
$cv->broadcast; # wake up wait'ers
The latter are a kind of tie into the main loop, and can be used to implement
blocking behaviour.
An example user is the Net::FCP module. It uses io-watchers for socket
communications and a condvar to signal result availability. It features an
autogenerated blocking API:
my $data = $fcp->client_get ($url);
Which just calls into the non-blocking API:
my $transaction = $fcp->txn_client_get ($url);
# returns very quickly
my $data = $transaction->result;
# blocks till data is available
This can be used to parallelize e.g. client_get requests easily:
my @datas = map $_->result,
map $fcp->txn_client_get ($_),
@urls;
All without having to specify an event model (or even having to think
about events when using the module): AnyEvent will automatically detect
wether a supported event module is loaded and will use it, and otherwise
will probe for a supported one. So the above "parallelizing" example will
work in a Gtk2, Tk and Event-based program.
Now all we would need is somebody who rewrites LWP into an AnyEvent-based
package...
(Also writing an implementation for other event loops such as Qt should be
very simple: the Event intefrace has 57 lines, while the Glib interface
(longest one) takes only 77 lines).
--
The choice of a
-----==- _GNU_
----==-- _ generation Marc Lehmann
---==---(_)__ __ ____ __ [EMAIL PROTECTED]
--==---/ / _ \/ // /\ \/ / http://schmorp.de/
-=====/_/_//_/\_,_/ /_/\_\ XX11-RIPE