| 1 | package Mojo::AsyncList; | ||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use Mojo::Base 'Mojo::EventEmitter'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Mojo::IOLoop; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Time::HiRes (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 1 | our $VERSION = '0.02'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 1 | has concurrent => 0; | |||||||||||||||||||||||||||
| 10 | 2 | ▶ | has ioloop => sub { Mojo::IOLoop->singleton }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 1 | has offset => 1; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 2 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||||||||||||||||
| 14 | 2 | my $class = shift; | |||||||||||||||||||||||||||
| 15 | 2 | ▶ | my $item_cb = ref $_[0] eq 'CODE' ? shift : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 16 | 2 | ▶ | my $finish_cb = ref $_[0] eq 'CODE' ? shift : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 17 | 2 | my $self = $class->SUPER::new(@_); | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 2 | ▶ | $self->on(item => $item_cb) if $item_cb; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 20 | 2 | ▶ | $self->on(finish => $finish_cb) if $finish_cb; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 2 | return $self; | |||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 2 | ▶ | sub process { | ||||||||||||||||||||||||||
|
Subroutine
process: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 26 | 2 | my ($self, $items) = @_; | |||||||||||||||||||||||||||
| 27 | 2 | my $remaining = int @$items; | |||||||||||||||||||||||||||
| 28 | 2 | my ($gather_cb, $item_pos, $pos, @res) = (undef, 0, 0); | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | my $stats = $self->{stats} | ||||||||||||||||||||||||||||
| 31 | 2 | = {done => 0, remaining => int(@$items), t0 => [Time::HiRes::gettimeofday]}; | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 4 | ▶ | $gather_cb = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 34 | 4 | my $res_pos = $pos++; | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 4 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 37 | 4 | shift for 1 .. $self->offset; | |||||||||||||||||||||||||||
| 38 | 4 | $stats->{done}++; | |||||||||||||||||||||||||||
| 39 | 4 | $stats->{remaining}--; | |||||||||||||||||||||||||||
| 40 | 4 | $res[$res_pos] = [@_]; | |||||||||||||||||||||||||||
| 41 | 4 | $self->emit(result => @_); | |||||||||||||||||||||||||||
| 42 | 4 | ▶ | return $self->emit(finish => @res) unless $stats->{remaining}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | 2 | ▶ | return $self->emit(item => $items->[$item_pos++], $gather_cb->()) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | if $item_pos < @$items; | ||||||||||||||||||||||||||||
| 45 | 4 | }; | |||||||||||||||||||||||||||
| 46 | 2 | }; | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 2 | ▶ | $self->ioloop->next_tick(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 49 | 2 | my $n = $self->concurrent; | |||||||||||||||||||||||||||
| 50 | 2 | ▶ | $n = @$items if !$n or $n > @$items; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $n or $n > @$items
MC/DC50%1/2
not $n or $n > @$items
not $n
$n > @$items
Truth table66%2/3
not(A) or B
A= not $nB= $n > @$items
| |||||||||||||||||||||||||||||
| 51 | 2 | $self->emit(item => $items->[$item_pos++], $gather_cb->()) for 1 .. $n; | |||||||||||||||||||||||||||
| 52 | 2 | }); | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 2 | return $self; | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 4 | ▶ | sub stats { | ||||||||||||||||||||||||||
|
Subroutine
stats: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||||||||
| 58 | 4 | my ($self, $key) = @_; | |||||||||||||||||||||||||||
| 59 | 4 | ▶ | return $key ? $self->{stats}{$key} // 0 : $self->{stats}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$self->{"stats"}{$key} // 0
MC/DC0%0/1
$self->{"stats"}{$key} // 0
$self->{"stats"}{$key}
Truth table50%1/2
A
A= $self->{"stats"}{$key}
| |||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 2 | ▶ | sub wait { | ||||||||||||||||||||||||||
|
Subroutine
wait: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 63 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 64 | 2 | ▶ | return if (my $loop = $self->ioloop)->is_running; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 2 | my $done; | |||||||||||||||||||||||||||
| 66 | 2 | ▶ | $self->on(finish => sub { $done++; $loop->stop }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 67 | 2 | ▶ | $loop->start until $done; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 1 | 1; | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | =head1 NAME | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | Mojo::AsyncList - Process a list with callbacks | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | use Mojo::AsyncList; | ||||||||||||||||||||||||||||
| 79 | use Mojo::mysql; | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | my $mysql = Mojo::mysql->new; | ||||||||||||||||||||||||||||
| 82 | my $db = $mysql->db; | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | my $async_list = Mojo::AsyncList->new( | ||||||||||||||||||||||||||||
| 85 | sub { # Specify a "item" event handler | ||||||||||||||||||||||||||||
| 86 | my ($async_list, $username, $gather_cb) = @_; | ||||||||||||||||||||||||||||
| 87 | $db->select("users", {username => $username}, $gather_cb); | ||||||||||||||||||||||||||||
| 88 | }, | ||||||||||||||||||||||||||||
| 89 | sub { # Specify a "finish" event handler | ||||||||||||||||||||||||||||
| 90 | my $async_list = shift; | ||||||||||||||||||||||||||||
| 91 | warn $_->[0]{user_id} for @_; # @_ = ([$db_res_supergirl], [$db_res_superman], ...) | ||||||||||||||||||||||||||||
| 92 | }, | ||||||||||||||||||||||||||||
| 93 | ); | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | my @users = qw(supergirl superman batman); | ||||||||||||||||||||||||||||
| 96 | $async_list->concurrent(2); | ||||||||||||||||||||||||||||
| 97 | $async_list->process(\@users); | ||||||||||||||||||||||||||||
| 98 | $async_list->wait; | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | L<Mojo::AsyncList> is a module that can asynchronously process a list of items | ||||||||||||||||||||||||||||
| 103 | with callback. | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | =head1 EVENTS | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | =head2 finish | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | $async_list->on(finish => sub { my ($async_list, @all_res) = @_; }); | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | Emitted when L</process> is done with all the C<$items>. C<@all_res> is a list | ||||||||||||||||||||||||||||
| 112 | of array-refs, where each item is C<@res> passed on to L</result>. | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | =head2 item | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | $async_list->on(item => sub { my ($async_list, $item, $gather_cb) = @_; }); | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | Used to process the next C<$item> in C<$items> passed on to L</process>. | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | =head2 result | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | $async_list->on(result => sub { my ($async_list, @res) = @_; }); | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | Emitted when a new result is ready, C<@res> contains the data passed on to | ||||||||||||||||||||||||||||
| 125 | C<$gather_cb>. | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | =head2 concurrent | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | $int = $async_list->concurrent; | ||||||||||||||||||||||||||||
| 132 | $async_list = $async_list->concurrent(0); | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | Used to set the number of concurrent items to process. Default value is zero, | ||||||||||||||||||||||||||||
| 135 | which means "process all items" at once. | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | Used to see how many items that is processing right now. | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | =head2 offset | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | $int = $async_list->offset; | ||||||||||||||||||||||||||||
| 142 | $async_list = $async_list->offset(1); | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | Will remove the number of arguments passed on to <$gather_cb>, used in the | ||||||||||||||||||||||||||||
| 145 | L</item> event. Default to "1", meaning it will remove the invocant. | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | =head1 METHODS | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | =head2 new | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | $async_list = Mojo::AsyncList->new; | ||||||||||||||||||||||||||||
| 152 | $async_list = Mojo::AsyncList->new(@attrs); | ||||||||||||||||||||||||||||
| 153 | $async_list = Mojo::AsyncList->new(\%attrs); | ||||||||||||||||||||||||||||
| 154 | $async_list = Mojo::AsyncList->new($item_cb, $finish_cb); | ||||||||||||||||||||||||||||
| 155 | $async_list = Mojo::AsyncList->new($item_cb, $finish_cb, \%attrs); | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | Used to create a new L<Mojo::AsyncList> object. L</item> and L<finish> event | ||||||||||||||||||||||||||||
| 158 | callbacks can be provided when constructing the object. | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | =head2 process | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | $async_list = $async_list->process(@items); | ||||||||||||||||||||||||||||
| 163 | $async_list = $async_list->process([@items]); | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | Process C<$items> and emit L</EVENTS> while doing so. | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | =head2 stats | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | $int = $async_list->stats("done"); | ||||||||||||||||||||||||||||
| 170 | $int = $async_list->stats("remaining"); | ||||||||||||||||||||||||||||
| 171 | $gettimeofday = $async_list->stats("t0"); | ||||||||||||||||||||||||||||
| 172 | $hash_ref = $async_list->stats; | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | Used to extract stats while items are processing. This can be useful inside the | ||||||||||||||||||||||||||||
| 175 | L</EVENTS>, or within a recurring timer: | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | Mojo::IOLoop->recurring(1 => sub { | ||||||||||||||||||||||||||||
| 178 | warn sprintf "[%s] done: %s\n", time, $async_list->stats("done"); | ||||||||||||||||||||||||||||
| 179 | }); | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | Changing the C<$hash_ref> will have fatal consequences. | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | =head2 wait | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | $async_list->concurrent(2)->process(\@items)->wait; | ||||||||||||||||||||||||||||
| 186 | $async_list->wait; | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | Used to block and wait until L<Mojo::AsyncList> is done with the C<$items> | ||||||||||||||||||||||||||||
| 189 | passed on to L</process>. | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | Jan Henning Thorsen | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | =cut | ||||||||||||||||||||||||||||