EV-Loop-Async

 view release on metacpan or  search on metacpan

Async.pm  view on Meta::CPAN

  
  # create a watcher, but make sure the loop is locked
  {
     $loop->scope_lock; # lock the loop structures
     $timer = $loop->timer (5, 1, sub { $flag = 1 });
     $loop->notify; # tell loop to take note of the timer
  }
  
  1 while $flag; # $flag will be set asynchronously
  
  # implement a critical section, uninterrupted by any callbacks
  {
     $loop->interrupt->scope_block;
     # critical section, no watcher callback interruptions
  }
  
  # stop the timer watcher again - locking is required once more
  {
     $loop->scope_lock; # lock the loop structures
     $timer->stop;
     # no need to notify
  }

=head1 DESCRIPTION

This module implements a rather specialised event loop - it takes a normal
L<EV> event loop and runs it in a separate thread. That means it will poll
for events even while your foreground Perl interpreter is busy (you don't
need to have perls pseudo-threads enabled for this either).

Whenever the event loop detecs new events, it will interrupt perl and ask
it to invoke all the pending watcher callbacks. This invocation will be
"synchronous" (in the perl thread), but it can happen at any time.

See the documentation for L<Async::Interrupt> for details on when and how
your perl program can be interrupted (and how to avoid it), and how to
integrate background event loops into foreground ones.

=head1 FAQ

=over 4

Async.pm  view on Meta::CPAN


=item $EV::Loop::Async::INTERRUPT

The default loop's L<Async::Interrupt> object, for easy access.

Example: create a section of code where no callback invocations will
interrupt:

   {
      $EV::Loop::Async::INTERRUPT->scope_block;
      # no default loop callbacks will be executed here.
      # the loop will not be locked, however.
   }

Example: embed the default EV::Async::Loop loop into the default L<EV>
loop (note that it could be any other event loop as well).

   my $async_w = EV::io
                    $EV::Loop::Async::LOOP->interrupt->pipe_fileno,
                    EV::READ,
                    sub { };

Async.pm  view on Meta::CPAN


=item $loop->set_max_foreground_loops ($max_loops)

The background loop will immediately stop polling for new events after it
has collected at least one new event, regardless of how long it then takes
to actually handle them.

When Perl finally handles the events, there could be many more ready
file descriptors. To improve latency and performance, you can ask
C<EV::Loop::Async> to loop an additional number of times in the foreground
after invoking the callbacks, effectively doing the polling in the
foreground.

The default is C<0>, meaning that no foreground polling will be done. A
value of C<1> means that, after handling the pending events, it will call
C<< $loop->loop (EV::LOOP_NONBLOCK) >> and handle the resulting events, if
any. A value of C<2> means that this will be iterated twice.

When a foreground event poll does not yield any new events, then no
further iterations will be made, so this is only a I<maximum> value of
additional loop runs.

README  view on Meta::CPAN

  
      # create a watcher, but make sure the loop is locked
      {
         $loop->scope_lock; # lock the loop structures
         $timer = $loop->timer (5, 1, sub { $flag = 1 });
         $loop->notify; # tell loop to take note of the timer
      }
  
      1 while $flag; # $flag will be set asynchronously
  
      # implement a critical section, uninterrupted by any callbacks
      {
         $loop->interrupt->scope_block;
         # critical section, no watcher callback interruptions
      }
  
      # stop the timer watcher again - locking is required once more
      {
         $loop->scope_lock; # lock the loop structures
         $timer->stop;
         # no need to notify
      }

DESCRIPTION
    This module implements a rather specialised event loop - it takes a
    normal EV event loop and runs it in a separate thread. That means it
    will poll for events even while your foreground Perl interpreter is busy
    (you don't need to have perls pseudo-threads enabled for this either).

    Whenever the event loop detecs new events, it will interrupt perl and
    ask it to invoke all the pending watcher callbacks. This invocation will
    be "synchronous" (in the perl thread), but it can happen at any time.

    See the documentation for Async::Interrupt for details on when and how
    your perl program can be interrupted (and how to avoid it), and how to
    integrate background event loops into foreground ones.

FAQ
    Why on earth...???
        Sometimes you need lower latency for specific events, but it's too
        heavy to continuously poll for events. And perl already does this

README  view on Meta::CPAN

        "EV::Loop::Async::default".

    $EV::Loop::Async::INTERRUPT
        The default loop's Async::Interrupt object, for easy access.

        Example: create a section of code where no callback invocations will
        interrupt:

           {
              $EV::Loop::Async::INTERRUPT->scope_block;
              # no default loop callbacks will be executed here.
              # the loop will not be locked, however.
           }

        Example: embed the default EV::Async::Loop loop into the default EV
        loop (note that it could be any other event loop as well).

           my $async_w = EV::io
                            $EV::Loop::Async::LOOP->interrupt->pipe_fileno,
                            EV::READ,
                            sub { };

README  view on Meta::CPAN

        current scope is left.

    $loop->set_max_foreground_loops ($max_loops)
        The background loop will immediately stop polling for new events
        after it has collected at least one new event, regardless of how
        long it then takes to actually handle them.

        When Perl finally handles the events, there could be many more ready
        file descriptors. To improve latency and performance, you can ask
        "EV::Loop::Async" to loop an additional number of times in the
        foreground after invoking the callbacks, effectively doing the
        polling in the foreground.

        The default is 0, meaning that no foreground polling will be done. A
        value of 1 means that, after handling the pending events, it will
        call "$loop->loop (EV::LOOP_NONBLOCK)" and handle the resulting
        events, if any. A value of 2 means that this will be iterated twice.

        When a foreground event poll does not yield any new events, then no
        further iterations will be made, so this is only a *maximum* value
        of additional loop runs.



( run in 1.737 second using v1.01-cache-2.11-cpan-9b1e4054eb1 )