EV
view release on metacpan or search on metacpan
BSDs). Modules should, unless they have "special needs" always use the
default loop as this is fastest (perl-wise), best supported by other
modules (e.g. AnyEvent or Coro) and most portable event loop.
For specific programs you can create additional event loops dynamically.
If you want to take advantage of kqueue (which often works properly for
sockets only) even though the default loop doesn't enable it, you can
*embed* a kqueue loop into the default loop: running the default loop
will then also service the kqueue loop to some extent. See the example
in the section about embed watchers for an example on how to achieve
that.
$loop = new EV::Loop [$flags]
Create a new event loop as per the specified flags. Please refer to
the "ev_loop_new ()" function description in the libev documentation
(<http://pod.tst.eu/http://cvs.schmorp.de/libev/ev.pod#GLOBAL_FUNCTI
ONS>, or locally-installed as EV::libev manpage) for more info.
The loop will automatically be destroyed when it is no longer
referenced by any watcher and the loop object goes out of scope.
If you are not embedding the loop, then using "EV::FLAG_FORKCHECK"
is recommended, as only the default event loop is protected by this
module. If you *are* embedding this loop in the default loop, this
is not necessary, as "EV::embed" automatically does the right thing
on fork.
$loop->loop_fork
Must be called after a fork in the child, before entering or
continuing the event loop. An alternative is to use
"EV::FLAG_FORKCHECK" which calls this function automatically, at
some performance loss (refer to the libev documentation).
$loop->verify
Calls "ev_verify" to make internal consistency checks (for debugging
libev) and abort the program if any data structures were found to be
corrupted.
$loop = EV::default_loop [$flags]
Return the default loop (which is a singleton object). Since this
module already creates the default loop with default flags,
specifying flags here will not have any effect unless you destroy
the default loop first, which isn't supported. So in short: don't do
it, and if you break it, you get to keep the pieces.
BASIC INTERFACE
$EV::DIED
Must contain a reference to a function that is called when a
callback throws an exception (with $@ containing the error). The
default prints an informative message and continues.
If this callback throws an exception it will be silently ignored.
$flags = EV::supported_backends
$flags = EV::recommended_backends
$flags = EV::embeddable_backends
Returns the set (see "EV::BACKEND_*" flags) of backends supported by
this instance of EV, the set of recommended backends (supposed to be
good) for this platform and the set of embeddable backends (see
EMBED WATCHERS).
EV::sleep $seconds
Block the process for the given number of (fractional) seconds.
$time = EV::time
Returns the current time in (fractional) seconds since the epoch.
$time = EV::now
$time = $loop->now
Returns the time the last event loop iteration has been started.
This is the time that (relative) timers are based on, and referring
to it is usually faster then calling EV::time.
EV::now_update
$loop->now_update
Establishes the current time by querying the kernel, updating the
time returned by "EV::now" in the progress. This is a costly
operation and is usually done automatically within "EV::run".
This function is rarely useful, but when some event callback runs
for a very long time without entering the event loop, updating
libev's idea of the current time is a good idea.
EV::suspend
$loop->suspend
EV::resume
$loop->resume
These two functions suspend and resume a loop, for use when the loop
is not used for a while and timeouts should not be processed.
A typical use case would be an interactive program such as a game:
When the user presses "^Z" to suspend the game and resumes it an
hour later it would be best to handle timeouts as if no time had
actually passed while the program was suspended. This can be
achieved by calling "suspend" in your "SIGTSTP" handler, sending
yourself a "SIGSTOP" and calling "resume" directly afterwards to
resume timer processing.
Effectively, all "timer" watchers will be delayed by the time spend
between "suspend" and "resume", and all "periodic" watchers will be
rescheduled (that is, they will lose any events that would have
occured while suspended).
After calling "suspend" you must not call *any* function on the
given loop other than "resume", and you must not call "resume"
without a previous call to "suspend".
Calling "suspend"/"resume" has the side effect of updating the event
loop time (see "now_update").
$backend = EV::backend
$backend = $loop->backend
Returns an integer describing the backend used by libev
(EV::BACKEND_SELECT or EV::BACKEND_EPOLL).
$active = EV::run [$flags]
$active = $loop->run ([$flags])
Begin checking for events and calling callbacks. It returns when a
callback calls EV::break or the flags are nonzero (in which case the
return value is true) or when there are no active watchers which
or return;
# make the dispatcher handle any outstanding stuff
... not shown
# create an I/O watcher for each and every socket
@snmp_watcher = (
(map { EV::io $_, EV::READ, sub { } }
keys %{ $dispatcher->{_descriptors} }),
EV::timer +($event->[Net::SNMP::Dispatcher::_ACTIVE]
? $event->[Net::SNMP::Dispatcher::_TIME] - EV::now : 0),
0, sub { },
);
};
The callbacks are irrelevant (and are not even being called), the
only purpose of those watchers is to wake up the process as soon as
one of those events occurs (socket readable, or timer timed out).
The corresponding EV::check watcher will then clean up:
our $snmp_check = EV::check sub {
# destroy all watchers
@snmp_watcher = ();
# make the dispatcher handle any new stuff
... not shown
};
The callbacks of the created watchers will not be called as the
watchers are destroyed before this can happen (remember EV::check
gets called first).
The "check_ns" variant doesn't start (activate) the newly created
watcher.
EV::CHECK constant issues
Like all other watcher types, there is a bitmask constant for use in
$revents and other places. The "EV::CHECK" is special as it has the
same name as the "CHECK" sub called by Perl. This doesn't cause big
issues on newer perls (beginning with 5.8.9), but it means thatthe
constant must be *inlined*, i.e. runtime calls will not work. That
means that as long as you always "use EV" and then "EV::CHECK" you
are on the safe side.
FORK WATCHERS - the audacity to resume the event loop after a fork
Fork watchers are called when a "fork ()" was detected. The invocation
is done before the event loop blocks next and before "check" watchers
are being called, and only in the child after the fork.
$w = EV::fork $callback
$w = EV::fork_ns $callback
$w = $loop->fork ($callback)
$w = $loop->fork_ns ($callback)
Call the callback before the event loop is resumed in the child
process after a fork.
The "fork_ns" variant doesn't start (activate) the newly created
watcher.
EMBED WATCHERS - when one backend isn't enough...
This is a rather advanced watcher type that lets you embed one event
loop into another (currently only IO events are supported in the
embedded loop, other types of watchers might be handled in a delayed or
incorrect fashion and must not be used).
See the libev documentation at
<http://pod.tst.eu/http://cvs.schmorp.de/libev/ev.pod#code_ev_embed_code
_when_one_backend_> (locally installed as EV::libev) for more details.
In short, this watcher is most useful on BSD systems without working
kqueue to still be able to handle a large number of sockets:
my $socket_loop;
# check wether we use SELECT or POLL _and_ KQUEUE is supported
if (
(EV::backend & (EV::BACKEND_POLL | EV::BACKEND_SELECT))
&& (EV::supported_backends & EV::embeddable_backends & EV::BACKEND_KQUEUE)
) {
# use kqueue for sockets
$socket_loop = new EV::Loop EV::BACKEND_KQUEUE | EV::FLAG_NOENV;
}
# use the default loop otherwise
$socket_loop ||= EV::default_loop;
$w = EV::embed $otherloop[, $callback]
$w = EV::embed_ns $otherloop[, $callback]
$w = $loop->embed ($otherloop[, $callback])
$w = $loop->embed_ns ($otherloop[, $callback])
Call the callback when the embedded event loop ($otherloop) has any
I/O activity. The $callback is optional: if it is missing, then the
embedded event loop will be managed automatically (which is
recommended), otherwise you have to invoke "sweep" yourself.
The "embed_ns" variant doesn't start (activate) the newly created
watcher.
ASYNC WATCHERS - how to wake up another event loop
Async watchers are provided by EV, but have little use in perl directly,
as perl neither supports threads running in parallel nor direct access
to signal handlers or other contexts where they could be of value.
It is, however, possible to use them from the XS level.
Please see the libev documentation for further details.
$w = EV::async $callback
$w = EV::async_ns $callback
$w = $loop->async ($callback)
$w = $loop->async_ns ($callback)
$w->send
$bool = $w->async_pending
CLEANUP WATCHERS - how to clean up when the event loop goes away
Cleanup watchers are not supported on the Perl level, they can only be
used via XS currently.
PERL SIGNALS
While Perl signal handling (%SIG) is not affected by EV, the behaviour
( run in 0.839 second using v1.01-cache-2.11-cpan-71847e10f99 )