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