libanyevent-aggressiveidle-perl-0.04/0000775000000000000000000000000011533455511016413 5ustar rootrootlibanyevent-aggressiveidle-perl-0.04/README0000644000000000000000000000226711527711141017275 0ustar rootrootAnyEvent-AggressiveIdle version 0.01 ==================================== The README is used to introduce the module and provide instructions on how to install the module, any machine dependencies it may have (for example C compilers and installed libraries) and any other information that should be provided before the module is installed. A README file is required for CPAN modules since CPAN extracts the README file from a module distribution so that people browsing the archive can use it get an idea of the modules uses. It is usually a good idea to provide version information here so that people can decide whether fixes for the module are worth downloading. INSTALLATION To install this module type the following: perl Makefile.PL make make test make install DEPENDENCIES This module requires these other modules and libraries: blah blah blah COPYRIGHT AND LICENCE Put the correct copyright and licence information here. Copyright (C) 2011 by Dmitry E. Oboukhov This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.10.1 or, at your option, any later version of Perl 5 you may have available. libanyevent-aggressiveidle-perl-0.04/Makefile.PL0000644000000000000000000000115011530007456020356 0ustar rootrootuse 5.010001; use ExtUtils::MakeMaker; # See lib/ExtUtils/MakeMaker.pm for details of how to influence # the contents of the Makefile that is written. WriteMakefile( NAME => 'AnyEvent::AggressiveIdle', VERSION_FROM => 'lib/AnyEvent/AggressiveIdle.pm', # finds $VERSION PREREQ_PM => { AnyEvent => 0, }, # e.g., Module::Name => 1.1 ($] >= 5.005 ? ## Add these new keywords supported since 5.005 (ABSTRACT_FROM => 'lib/AnyEvent/AggressiveIdle.pm', # retrieve abstract from module AUTHOR => 'Dmitry E. Oboukhov ') : ()), ); libanyevent-aggressiveidle-perl-0.04/t/0000775000000000000000000000000011533135653016660 5ustar rootrootlibanyevent-aggressiveidle-perl-0.04/t/01_base.t0000644000000000000000000000324311530007232020243 0ustar rootroot#!/usr/bin/perl use warnings; use strict; use utf8; use open qw(:std :utf8); use lib qw(lib ../lib); use Test::More tests => 6; use Encode qw(decode encode); BEGIN { # Подготовка объекта тестирования для работы с utf8 my $builder = Test::More->builder; binmode $builder->output, ":utf8"; binmode $builder->failure_output, ":utf8"; binmode $builder->todo_output, ":utf8"; # use_ok 'AnyEvent::Impl::EV'; use_ok 'AnyEvent'; 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"; } libanyevent-aggressiveidle-perl-0.04/t/02_guards.t0000644000000000000000000000220011533135653020623 0ustar rootroot#!/usr/bin/perl use warnings; use strict; use utf8; use open qw(:std :utf8); use lib qw(lib ../lib); use Test::More tests => 6; use Encode qw(decode encode); BEGIN { my $builder = Test::More->builder; binmode $builder->output, ":utf8"; binmode $builder->failure_output, ":utf8"; binmode $builder->todo_output, ":utf8"; use_ok 'AnyEvent'; use_ok 'AnyEvent::AggressiveIdle', ':all'; } { my $cv = condvar AnyEvent; my ($cnt1, $cnt2, $cnt2_t, $cnt3) = (0, 0, 0, 0); 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; ok $cnt1 == $cnt3, 'Two aggressive_idle counters are equal'; ok $cnt1 > $cnt2, 'Timer works properly'; ok $cnt2_t == 10, 'Subroutine was called 10 times'; ok abs($cnt2 - $cnt2_t) <= 1, 'Idle process was called 10 or 11 times'; } libanyevent-aggressiveidle-perl-0.04/MANIFEST0000664000000000000000000000025111533137665017551 0ustar rootrootChanges debian/changelog debian/compat debian/control debian/copyright debian/rules lib/AnyEvent/AggressiveIdle.pm Makefile.PL MANIFEST README t/01_base.t t/02_guards.t libanyevent-aggressiveidle-perl-0.04/Changes0000644000000000000000000000103511533136527017706 0ustar rootrootRevision history for Perl extension AnyEvent::AggressiveIdle. 0.01 Sat Feb 19 12:50:25 2011 - original version; created by h2xs 1.23 with options -X -n AnyEvent::AggressiveIdle 0.02 Sat Feb 19 21:42:00 MSK 2011 - AnyEvent::Impl::EV works properly. - Add AnyEvent in Depends 0.03 Sun Feb 20 12:09:52 MSK 2011 - use pipe instead file. Thanks for Marc Lehmann's review. 0.04 Tue Mar 1 12:26:24 MSK 2011 - Add a guard into idle callback: You can do something continuous inside the callback. libanyevent-aggressiveidle-perl-0.04/lib/0000775000000000000000000000000011527711141017156 5ustar rootrootlibanyevent-aggressiveidle-perl-0.04/lib/AnyEvent/0000775000000000000000000000000011533137335020713 5ustar rootrootlibanyevent-aggressiveidle-perl-0.04/lib/AnyEvent/AggressiveIdle.pm0000644000000000000000000001033611533137335024147 0ustar rootrootpackage AnyEvent::AggressiveIdle; use Carp; use AnyEvent; use AnyEvent::Util; use 5.010001; use strict; use warnings; require Exporter; our @ISA = qw(Exporter); our $VERSION = '0.04'; our @EXPORT = qw(aggressive_idle); our @EXPORT_OK = qw(stop_aggressive_idle aggressive_idle); our %EXPORT_TAGS = ( all => [@EXPORT_OK] ); sub stop_aggressive_idle($) { our (%IDLE, $WATCHER); my ($no) = @_; croak "Invalid idle identifier: $no" unless $no and !ref($no) and exists $IDLE{$no}; delete $IDLE{$no}; undef $WATCHER unless %IDLE; return; } sub _watcher { our ($WATCHER, $WOBJ, %IDLE); # localize keys (because idle processes can change # watchers list) my @pid = keys %IDLE; for my $p (@pid) { next unless exists $IDLE{$p}; next unless defined $IDLE{$p}; my $cb = $IDLE{$p}; $IDLE{$p} = undef; my $done = 0; { my $guard = guard { $done = 1; # do not restart idle process if user has stopped it if (exists $IDLE{$p}) { $IDLE{$p} = $cb; return if $WATCHER; $WATCHER = AE::io $WOBJ, 1, \&_watcher; } }; $cb->($p, $guard); } unless ($done) { undef $WATCHER unless %IDLE; } } } sub aggressive_idle(&) { our ($WOBJ, $WOBJR, %IDLE, $WATCHER, $NO); ($WOBJR, $WOBJ) = portable_pipe unless defined $WOBJ; $NO = 0 unless defined $NO; $WATCHER = AE::io $WOBJ, 1, \&_watcher unless %IDLE; my $no = ++$NO; $IDLE{$no} = $_[0]; return unless defined wantarray; return guard { stop_aggressive_idle $no }; } 1; __END__ =head1 NAME AnyEvent::AggressiveIdle - Aggressive idle processes for AnyEvent. =head1 SYNOPSIS use AnyEvent::AggressiveIdle qw(aggressive_idle}; aggressive_idle { ... do something important }; my $idle; $idle = aggressive_idle { ... do something important if (FINISH) { undef $idle; # do not call the sub anymore } }; =head1 DESCRIPTION Sometimes You need to do something that takes much time but can be split into elementary phases. If You use L and Your program is a highload project, idle process can be delayed for much time (second, hour, day, etc). L will be called for each L loop cycle. So You can be sure that Your idle process will continue. =head1 EXPORTS =head2 aggressive_idle Register Your function as aggressive idle watcher. If it is called in B context, the watcher wont be deinstalled. Be carrefully. In B context the function returns a L. Hold the guard until You want to cancel idle process. =head2 stop_aggressive_idle You can use the function to stop idle process. The function receives idle process B that can be received in idle callback (the first argument). Example: use AnyEvent::AggressiveIdle ':all'; # or: use AnyEvent::AggressiveIdle qw(aggressive_idle stop_aggressive_idle); aggressive_idle { my ($pid) = @_; .... stop_aggressive_idle $pid; } 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' aggressive_idle won't call its callback. Feel free to L before free the guard. =head1 AUTHOR Dmitry E. Oboukhov, Eunera@debian.orgE =head1 COPYRIGHT AND LICENSE Copyright (C) 2011 by Dmitry E. Oboukhov This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.10.1 or, at your option, any later version of Perl 5 you may have available. =head1 VCS The project is placed on my GIT repo: L =cut