| 1 |
NAME |
| 2 |
"Async::MergePoint" - resynchronise diverged control flow |
| 3 |
|
| 4 |
SYNOPSIS |
| 5 |
use Async::MergePoint; |
| 6 |
|
| 7 |
my $merge = Async::MergePoint->new( |
| 8 |
needs => [ "leaves", "water" ], |
| 9 |
); |
| 10 |
|
| 11 |
my $water; |
| 12 |
Kettle->boil( |
| 13 |
on_boiled => sub { $water = shift; $merge->done( "water" ); } |
| 14 |
); |
| 15 |
|
| 16 |
my $tea_leaves; |
| 17 |
Cupboard->get_tea_leaves( |
| 18 |
on_fetched => sub { $tea_leaves = shift; $merge->done( "leaves" ); } |
| 19 |
); |
| 20 |
|
| 21 |
$merge->close( |
| 22 |
on_finished => sub { |
| 23 |
# Make tea using $water and $tea_leaves |
| 24 |
} |
| 25 |
); |
| 26 |
|
| 27 |
DESCRIPTION |
| 28 |
Often in program logic, multiple different steps need to be taken that |
| 29 |
are independent of each other, but their total result is needed before |
| 30 |
the next step can be taken. In synchonous code, the usual approach is to |
| 31 |
do them sequentially. |
| 32 |
|
| 33 |
An asynchronous or event-based program could do this, but if each step |
| 34 |
involves some IO idle time, better overall performance can often be |
| 35 |
gained by running the steps in parallel. A "Async::MergePoint" object |
| 36 |
can then be used to wait for all of the steps to complete, before |
| 37 |
passing the combined result of each step on to the next stage. |
| 38 |
|
| 39 |
A merge point maintains a set of outstanding operations it is waiting |
| 40 |
on; these are arbitrary string values provided at the object's |
| 41 |
construction. Each time the "done()" method is called, the named item is |
| 42 |
marked as being complete. When all of the required items are so marked, |
| 43 |
the "on_finished" continuation is invoked. |
| 44 |
|
| 45 |
For use cases where code may be split across several different lexical |
| 46 |
scopes, it may not be convenient or possible to share a lexical |
| 47 |
variable, to pass on the result of some asynchronous operation. In these |
| 48 |
cases, when an item is marked as complete a value can also be provided |
| 49 |
which contains the results of that step. The "on_finished" callback is |
| 50 |
passed a hash (in list form, rather than by reference) of the collected |
| 51 |
item values. |
| 52 |
|
| 53 |
This module was originally part of the IO::Async distribution, but was |
| 54 |
removed under the inspiration of Pedro Melo's Async::Hooks distribution, |
| 55 |
because it doesn't itself contain anything IO-specific. |
| 56 |
|
| 57 |
CONSTRUCTOR |
| 58 |
$merge = Async::MergePoint->new( %params ) |
| 59 |
This function returns a new instance of a "Async::MergePoint" object. |
| 60 |
The %params hash takes the following keys: |
| 61 |
|
| 62 |
needs => ARRAY |
| 63 |
Optional. An array containing unique item names to wait on. The |
| 64 |
order of this array is not significant. |
| 65 |
|
| 66 |
on_finished => CODE |
| 67 |
Optional. CODE reference to the continuation for when the merge |
| 68 |
point becomes ready. If provided, will be passed to the "close" |
| 69 |
method. |
| 70 |
|
| 71 |
METHODS |
| 72 |
$merge->close( %params ) |
| 73 |
Allows an "on_finished" continuation to be set if one was not provided |
| 74 |
to the constructor. |
| 75 |
|
| 76 |
on_finished => CODE |
| 77 |
CODE reference to the continuation for when the merge point |
| 78 |
becomes ready. |
| 79 |
|
| 80 |
The "on_finished" continuation will be called when every key in the |
| 81 |
"needs" list has been notified by the "done()" method. It will be called |
| 82 |
as |
| 83 |
|
| 84 |
$on_finished->( %items ) |
| 85 |
|
| 86 |
where the %items hash will contain the item names that were waited on, |
| 87 |
and the values passed to the "done()" method for each one. Note that |
| 88 |
this is passed as a list, not as a HASH reference. |
| 89 |
|
| 90 |
While this feature can be used to pass data from the component parts |
| 91 |
back up into the continuation, it may be more direct to use normal |
| 92 |
lexical variables instead. This method allows the continuation to be |
| 93 |
placed after the blocks of code that execute the component parts, so it |
| 94 |
reads downwards, and may make it more readable. |
| 95 |
|
| 96 |
$merge->needs( @keys ) |
| 97 |
When called on an open MergePoint (i.e. one that does not yet have an |
| 98 |
"on_finished" continuation), this method adds extra key names to the set |
| 99 |
of outstanding names. The order of this list is not significant. |
| 100 |
|
| 101 |
This method throws an exception if the MergePoint is already closed. |
| 102 |
|
| 103 |
$merge->done( $item, $value ) |
| 104 |
This method informs the merge point that the $item is now ready, and |
| 105 |
passes it a value to store, to be passed into the "on_finished" |
| 106 |
continuation. If this call gives the final remaining item being waited |
| 107 |
for, the "on_finished" continuation is called within it, and the method |
| 108 |
will not return until it has completed. |
| 109 |
|
| 110 |
EXAMPLES |
| 111 |
Asynchronous Plugins |
| 112 |
Consider a program using "Module::Pluggable" to provide a plugin |
| 113 |
architecture to respond to events, where sometimes the response to an |
| 114 |
event may require asynchronous work. A "MergePoint" object can be used |
| 115 |
to coordinate the responses from the plugins to this event. |
| 116 |
|
| 117 |
my $merge = Async::MergePoint->new(); |
| 118 |
|
| 119 |
foreach my $plugin ( $self->plugins ) { |
| 120 |
$plugin->handle_event( "event", $merge, @args ); |
| 121 |
} |
| 122 |
|
| 123 |
$merge->close( on_finished => sub { |
| 124 |
my %results = @_; |
| 125 |
print "All plugins have recognised $event\n"; |
| 126 |
} ); |
| 127 |
|
| 128 |
Each plugin that wishes to handle the event can use its own package |
| 129 |
name, for example, as its unique key name for the MergePoint. A plugin |
| 130 |
handling the event synchonously could perform something such as: |
| 131 |
|
| 132 |
sub handle_event |
| 133 |
{ |
| 134 |
my ( $event, $merge, @args ) = @_; |
| 135 |
.... |
| 136 |
$merge->needs( __PACKAGE__ ); |
| 137 |
$merge->done( __PACKAGE__ => $result ); |
| 138 |
} |
| 139 |
|
| 140 |
Whereas, to handle the event asynchronously the plugin can instead |
| 141 |
perform: |
| 142 |
|
| 143 |
sub handle_event |
| 144 |
{ |
| 145 |
my ( $event, $merge, @args ) = @_; |
| 146 |
.... |
| 147 |
$merge->needs( __PACKAGE__ ); |
| 148 |
|
| 149 |
sometime_later( sub { |
| 150 |
$merge->done( __PACKAGE__ => $result ); |
| 151 |
} ); |
| 152 |
} |
| 153 |
|
| 154 |
AUTHOR |
| 155 |
Paul Evans <[email protected]> |
| 156 |
|