Last active
March 11, 2017 03:58
-
-
Save jberger/6960933d2a8355ff878d64df344ffa51 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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