Skip to content

Instantly share code, notes, and snippets.

@jberger
Last active March 11, 2017 03:58
Show Gist options
  • Select an option

  • Save jberger/6960933d2a8355ff878d64df344ffa51 to your computer and use it in GitHub Desktop.

Select an option

Save jberger/6960933d2a8355ff878d64df344ffa51 to your computer and use it in GitHub Desktop.
use Mojo::Base -strict;
use feature 'current_sub';
use Mojo::IOLoop;
my $concurrency = 100;
# this is dummy data, fill it with your data to be processed
my @items = (1..1000);
sub do_batch {
my $cb = shift;
Mojo::IOLoop->delay(
sub {
my $delay = shift;
for (1..$concurrency) {
last unless @items;
my $item = shift @items;
my $end = $delay->begin;
# this is a dummy, replace with your actual work
Mojo::IOLoop->timer(1 => sub { say $item; $end->() });
}
},
sub { $cb->() },
);
}
# this callback basically reschedules itself (trampolines) until done
do_batch(sub{ @items ? do_batch(__SUB__) : say 'done' });
Mojo::IOLoop->start;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment