EV

 view release on metacpan or  search on metacpan

EV.pm  view on Meta::CPAN


=item $loop = new EV::Loop [$flags]

Create a new event loop as per the specified flags. Please refer to
the C<ev_loop_new ()> function description in the libev documentation
(L<http://pod.tst.eu/http://cvs.schmorp.de/libev/ev.pod#GLOBAL_FUNCTIONS>,
or locally-installed as F<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 C<EV::FLAG_FORKCHECK>
is recommended, as only the default event loop is protected by this
module. If you I<are> embedding this loop in the default loop, this is not
necessary, as C<EV::embed> automatically does the right thing on fork.

=item $loop->loop_fork

Must be called after a fork in the child, before entering or continuing
the event loop. An alternative is to use C<EV::FLAG_FORKCHECK> which calls
this function automatically, at some performance loss (refer to the libev
documentation).

=item $loop->verify

Calls C<ev_verify> to make internal consistency checks (for debugging
libev) and abort the program if any data structures were found to be
corrupted.

=item $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.

=back


=head1 BASIC INTERFACE

=over 4

=item $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.

=item $flags = EV::supported_backends

=item $flags = EV::recommended_backends

=item $flags = EV::embeddable_backends

Returns the set (see C<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).

=item EV::sleep $seconds

Block the process for the given number of (fractional) seconds.

=item $time = EV::time

Returns the current time in (fractional) seconds since the epoch.

=item $time = EV::now

=item $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.

=item EV::now_update

=item $loop->now_update

Establishes the current time by querying the kernel, updating the time
returned by C<EV::now> in the progress. This is a costly operation and
is usually done automatically within C<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.

=item EV::suspend

=item $loop->suspend

=item EV::resume

=item $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 C<^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 C<suspend>
in your C<SIGTSTP> handler, sending yourself a C<SIGSTOP> and calling
C<resume> directly afterwards to resume timer processing.

Effectively, all C<timer> watchers will be delayed by the time spend
between C<suspend> and C<resume>, and all C<periodic> watchers
will be rescheduled (that is, they will lose any events that would have
occured while suspended).

After calling C<suspend> you B<must not> call I<any> function on the given
loop other than C<resume>, and you B<must not> call C<resume>
without a previous call to C<suspend>.

Calling C<suspend>/C<resume> has the side effect of updating the event
loop time (see C<now_update>).

=item $backend = EV::backend

EV.pm  view on Meta::CPAN

                    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 C<check_ns> variant doesn't start (activate) the newly created watcher.

=item EV::CHECK constant issues

Like all other watcher types, there is a bitmask constant for use in
C<$revents> and other places. The C<EV::CHECK> is special as it has
the same name as the C<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 I<inlined>, i.e. runtime calls will not work. That means
that as long as you always C<use EV> and then C<EV::CHECK> you are on the
safe side.

=back


=head3 FORK WATCHERS - the audacity to resume the event loop after a fork

Fork watchers are called when a C<fork ()> was detected. The invocation
is done before the event loop blocks next and before C<check> watchers
are being called, and only in the child after the fork.

=over 4

=item $w = EV::fork $callback

=item $w = EV::fork_ns $callback

=item $w = $loop->fork ($callback)

=item $w = $loop->fork_ns ($callback)

Call the callback before the event loop is resumed in the child process
after a fork.

The C<fork_ns> variant doesn't start (activate) the newly created watcher.

=back


=head3 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
L<http://pod.tst.eu/http://cvs.schmorp.de/libev/ev.pod#code_ev_embed_code_when_one_backend_>
(locally installed as F<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;

=over 4

=item $w = EV::embed $otherloop[, $callback]

=item $w = EV::embed_ns $otherloop[, $callback]

=item $w = $loop->embed ($otherloop[, $callback])

=item $w = $loop->embed_ns ($otherloop[, $callback])

Call the callback when the embedded event loop (C<$otherloop>) has any
I/O activity. The C<$callback> is optional: if it is missing, then the
embedded event loop will be managed automatically (which is recommended),
otherwise you have to invoke C<sweep> yourself.

The C<embed_ns> variant doesn't start (activate) the newly created watcher.

=back

=head3 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.

=over 4

=item $w = EV::async $callback



( run in 0.746 second using v1.01-cache-2.11-cpan-71847e10f99 )