MojoX::IOLoop::Throttle is a Perl module to throttle Mojo events.
SYNOPSIS
use Mojo::Base -strict;
use MojoX::IOLoop::Throttle;
$| = 1;
# New throttle object
my $throttle = MojoX::IOLoop::Throttle->new(
limit_run =>
3, # Allow not more than [limit_run] running (parallel,incomplete) jobs
period => 2, # seconds
limit_period =>
4, # do not start more than [limit_period] jobs per [period] seconds
delay => 0.05 # Simulate a little latency
);
my $count;
# Subscribe to finish event
$throttle->on(finish =>
sub { say "I've processed $count jobs! Bye-bye"; Mojo::IOLoop->stop; });
# Throttle 20 jobs!
$throttle->add_limit(20);
# CallBack to throttle
$throttle->run(
sub {
my ($thr) = @_;
my $rand_time = rand() / 5;
say "Job $rand_time started";
$thr->ioloop->timer(
$rand_time => sub {
say "job $rand_time ended";
$count++;
# Say that we end (to decrease limit_run count and let other job to start)
$thr->end();
}
);
}
);
# Let's start
Mojo::IOLoop->start;
Product's homepage
Requirements:
· Perl