AnyEvent-AggressiveIdle

 view release on metacpan or  search on metacpan

lib/AnyEvent/AggressiveIdle.pm  view on Meta::CPAN

The function will throw an exception if invalid PID is received.

=head1 Continuous process.

Sometimes You need to to something continuous inside idle callback. If
You want to stop idle calls until You have done Your work, You can hold
guard inside Your process:

    aggressive_idle {
        my ($pid, $guard) = @_;
        my $timer;
        $timer = AE::timer 0.5, 0 => sub {
            undef $timer;
            undef $guard;   # POINT 1
        }
    }

Until 'B<POINT 1>' aggressive_idle won't call its callback.
Feel free to L<stop_aggressive_idle> before free the guard.

=head1 AUTHOR

Dmitry E. Oboukhov, E<lt>unera@debian.orgE<gt>

t/01_base.t  view on Meta::CPAN

    use_ok 'AnyEvent::AggressiveIdle';
}


{
    my $cv = condvar AnyEvent;
    my ($common_counter, $aggressive_counter) = (0, 0);

    my $common_idle;
    my $aggressive_idle;
    my $timer;

    $common_idle = AE::idle sub { $common_counter++ };
    $aggressive_idle = aggressive_idle { $aggressive_counter++ };
    $timer = AE::timer 0.5, 0 => sub { $cv->send };

    $cv->recv;

    diag explain [ $aggressive_counter, $common_counter ]
        unless ok $aggressive_counter > $common_counter,
            "aggressive_idle works fine";
    diag explain [ $aggressive_counter, $common_counter ]
        unless ok $common_counter < 3, "aggressive_idle blocks AE::idle";
}
{
    my $cv = condvar AnyEvent;
    my $counter = 0;
    my $counter2 = 0;
    my $idle;
    $idle = aggressive_idle { undef $idle if ++$counter >= 1000 };

    my $t0 = AE::timer 0.5, 0 => sub {
        aggressive_idle { ++$counter2 };
        return;
    };
    my $timer = AE::timer 1, 0 => sub { $cv->send };
    $cv->recv;

    diag $counter unless ok $counter == 1000, "Breaking idle process";
    diag explain [ $counter2, $counter ]
        unless ok $counter2 > $counter, "Breaknig inside idle process";
}

t/02_guards.t  view on Meta::CPAN

    my $idle1 = aggressive_idle {
        $cnt1++;

    };

    my $idle2 = aggressive_idle {
        my ($pid, $guard) = @_;

        $cnt2++;
        my $t;
        $t = AE::timer 0.1, 0 => sub {
            undef $t;
            $cnt2_t++;
            $cv->send if $cnt2 == 10;
            undef $guard;
        }
    };

    my $idle3 = aggressive_idle { $cnt3++ };

    $cv->recv;



( run in 1.582 second using v1.01-cache-2.11-cpan-49f99fa48dc )