| 1 | package Async::Simple::Task; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | Async::Simple::Task - base class for asyncronous task packages | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | use Async::Simple::Task::ChildPkg; | ||
| 10 | |||
| 11 | my $task = Async::Simple::Task::ChildPkg->new( %params ); # Creates a task, which waits for data and doing something with it | ||
| 12 | |||
| 13 | $task->put( $data ); # Put a task data to task | ||
| 14 | |||
| 15 | # ...do something useful in parent while our data working ... | ||
| 16 | |||
| 17 | my $result = $task->get; # result = undef because result is not ready yet | ||
| 18 | sleep $timeout; # or do something else.... | ||
| 19 | my $result = $task->get; # your result | ||
| 20 | |||
| 21 | $task->put( $data ); # Put another data and so on,.... | ||
| 22 | |||
| 23 | Result and data can be of any type you wish. | ||
| 24 | |||
| 25 | If your "get" can return undef as result, you should check $task->has_result, as a mark that result is ready. | ||
| 26 | |||
| 27 | |||
| 28 | =head1 DESCRIPTION | ||
| 29 | |||
| 30 | Allows to initialize async process. | ||
| 31 | |||
| 32 | After that, puts to him many similar packs of data one after other. | ||
| 33 | |||
| 34 | |||
| 35 | =head1 METHODS | ||
| 36 | |||
| 37 | =head2 C<new> | ||
| 38 | |||
| 39 | Initialize async task routine. | ||
| 40 | |||
| 41 | my $task = Async::Simple::Task::ChildPkg->new( %optional_params ); | ||
| 42 | |||
| 43 | |||
| 44 | =head2 C<put> | ||
| 45 | |||
| 46 | Puts data to task | ||
| 47 | |||
| 48 | $self->put( $data ); | ||
| 49 | |||
| 50 | |||
| 51 | =head2 C<get> | ||
| 52 | |||
| 53 | Tries to read result from task. | ||
| 54 | |||
| 55 | Returns result or undef in case when result is not ready. | ||
| 56 | |||
| 57 | In case, your function can return undef as result, | ||
| 58 | you shoud check $task->has_answer, as a mark of ready result. | ||
| 59 | |||
| 60 | my $result = $self->get(); | ||
| 61 | |||
| 62 | |||
| 63 | =head1 SUPPORT AND DOCUMENTATION | ||
| 64 | |||
| 65 | After installing, you can find documentation for this module with the perldoc command. | ||
| 66 | |||
| 67 | perldoc Async::Simple::Task | ||
| 68 | |||
| 69 | You can also look for information at: | ||
| 70 | |||
| 71 | RT, CPAN's request tracker (report bugs here) | ||
| 72 | http://rt.cpan.org/NoAuth/Bugs.html?Dist=Async-Simple-Task | ||
| 73 | |||
| 74 | AnnoCPAN, Annotated CPAN documentation | ||
| 75 | http://annocpan.org/dist/Async-Simple-Task | ||
| 76 | |||
| 77 | CPAN Ratings | ||
| 78 | http://cpanratings.perl.org/d/Async-Simple-Task | ||
| 79 | |||
| 80 | Search CPAN | ||
| 81 | http://search.cpan.org/dist/Async-Simple-Task/ | ||
| 82 | |||
| 83 | |||
| 84 | =head1 AUTHOR | ||
| 85 | |||
| 86 | ANTONC <antonc@cpan.org> | ||
| 87 | |||
| 88 | |||
| 89 | =head1 LICENSE | ||
| 90 | |||
| 91 | This program is free software; you can redistribute it and/or modify it | ||
| 92 | under the terms of the the Artistic License (2.0). You may obtain a | ||
| 93 | copy of the full license at: | ||
| 94 | |||
| 95 | L<http://www.perlfoundation.org/artistic_license_2_0> | ||
| 96 | |||
| 97 | =cut | ||
| 98 | |||
| 99 | |||
| 100 | 8 | ▶ | use Modern::Perl; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 101 | 8 | ▶ | use Moose; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 102 | 8 | ▶ | use namespace::autoclean; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 103 | |||
| 104 | 8 | our $VERSION = '0.18'; | |
| 105 | |||
| 106 | |||
| 107 | =head1 Attributes | ||
| 108 | |||
| 109 | =head2 get | ||
| 110 | |||
| 111 | my $result = $task->get; | ||
| 112 | |||
| 113 | Reads from task, if something can be readed or returns undef after timeout. | ||
| 114 | |||
| 115 | You should override this. | ||
| 116 | |||
| 117 | =cut | ||
| 118 | |||
| 119 | |||
| 120 | =head2 put | ||
| 121 | |||
| 122 | $task->put( $data ); | ||
| 123 | |||
| 124 | Makes task. | ||
| 125 | |||
| 126 | You should override this. | ||
| 127 | |||
| 128 | =cut | ||
| 129 | |||
| 130 | |||
| 131 | =head2 answer | ||
| 132 | |||
| 133 | Result of current task | ||
| 134 | |||
| 135 | =cut | ||
| 136 | |||
| 137 | |||
| 138 | 8 | has answer => ( | |
| 139 | is => 'rw', | ||
| 140 | isa => 'Any', | ||
| 141 | predicate => 'has_answer', | ||
| 142 | clearer => 'clear_answer', | ||
| 143 | ); | ||
| 144 | |||
| 145 | |||
| 146 | =head2 has_answer | ||
| 147 | |||
| 148 | has_answer is true, if the task has been finished and result is ready. | ||
| 149 | |||
| 150 | =cut | ||
| 151 | |||
| 152 | |||
| 153 | =head2 timeout | ||
| 154 | |||
| 155 | timeout - positive numeric value = seconds between checking for result | ||
| 156 | |||
| 157 | =cut | ||
| 158 | |||
| 159 | 8 | has timeout => ( | |
| 160 | is => 'ro', | ||
| 161 | isa => 'Num', | ||
| 162 | required => 1, | ||
| 163 | default => 0.01, | ||
| 164 | ); | ||
| 165 | |||
| 166 | |||
| 167 | =head2 id | ||
| 168 | |||
| 169 | Index of current task task. | ||
| 170 | |||
| 171 | This fields is just for your purpose, it is not intersected with any of internal logic. | ||
| 172 | |||
| 173 | Use id as a unique marker of task, in casw when you have a list of similar tasks. | ||
| 174 | |||
| 175 | =cut | ||
| 176 | |||
| 177 | 8 | has id => ( | |
| 178 | is => 'rw', | ||
| 179 | isa => 'Str', | ||
| 180 | predicate => 'has_id', | ||
| 181 | clearer => 'clear_id', | ||
| 182 | ); | ||
| 183 | |||
| 184 | |||
| 185 | 8 | __PACKAGE__->meta->make_immutable; | |