.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.40) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). .\" Fear. Run. Save yourself. No user-serviceable parts. . \" fudge factors for nroff and troff .if n \{\ . ds #H 0 . ds #V .8m . ds #F .3m . ds #[ \f1 . ds #] \fP .\} .if t \{\ . ds #H ((1u-(\\\\n(.fu%2u))*.13m) . ds #V .6m . ds #F 0 . ds #[ \& . ds #] \& .\} . \" simple accents for nroff and troff .if n \{\ . ds ' \& . ds ` \& . ds ^ \& . ds , \& . ds ~ ~ . ds / .\} .if t \{\ . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' .\} . \" troff and (daisy-wheel) nroff accents .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' .ds 8 \h'\*(#H'\(*b\h'-\*(#H' .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] .ds ae a\h'-(\w'a'u*4/10)'e .ds Ae A\h'-(\w'A'u*4/10)'E . \" corrections for vroff .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' . \" for low resolution devices (crt and lpr) .if \n(.H>23 .if \n(.V>19 \ \{\ . ds : e . ds 8 ss . ds o a . ds d- d\h'-1'\(ga . ds D- D\h'-1'\(hy . ds th \o'bp' . ds Th \o'LP' . ds ae ae . ds Ae AE .\} .rm #[ #] #H #V #F C .\" ======================================================================== .\" .IX Title "Async::MergePoint 3pm" .TH Async::MergePoint 3pm "2020-12-25" "perl v5.32.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" "Async::MergePoint" \- resynchronise diverged control flow .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 1 \& use Async::MergePoint; \& \& my $merge = Async::MergePoint\->new( \& needs => [ "leaves", "water" ], \& ); \& \& my $water; \& Kettle\->boil( \& on_boiled => sub { $water = shift; $merge\->done( "water" ); } \& ); \& \& my $tea_leaves; \& Cupboard\->get_tea_leaves( \& on_fetched => sub { $tea_leaves = shift; $merge\->done( "leaves" ); } \& ); \& \& $merge\->close( \& on_finished => sub { \& # Make tea using $water and $tea_leaves \& } \& ); .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" Often in program logic, multiple different steps need to be taken that are independent of each other, but their total result is needed before the next step can be taken. In synchonous code, the usual approach is to do them sequentially. .PP An asynchronous or event-based program could do this, but if each step involves some \s-1IO\s0 idle time, better overall performance can often be gained by running the steps in parallel. A \f(CW\*(C`Async::MergePoint\*(C'\fR object can then be used to wait for all of the steps to complete, before passing the combined result of each step on to the next stage. .PP A merge point maintains a set of outstanding operations it is waiting on; these are arbitrary string values provided at the object's construction. Each time the \f(CW\*(C`done()\*(C'\fR method is called, the named item is marked as being complete. When all of the required items are so marked, the \f(CW\*(C`on_finished\*(C'\fR continuation is invoked. .PP For use cases where code may be split across several different lexical scopes, it may not be convenient or possible to share a lexical variable, to pass on the result of some asynchronous operation. In these cases, when an item is marked as complete a value can also be provided which contains the results of that step. The \f(CW\*(C`on_finished\*(C'\fR callback is passed a hash (in list form, rather than by reference) of the collected item values. .PP This module was originally part of the IO::Async distribution, but was removed under the inspiration of Pedro Melo's Async::Hooks distribution, because it doesn't itself contain anything IO-specific. .SH "CONSTRUCTOR" .IX Header "CONSTRUCTOR" .ie n .SS "$merge = Async::MergePoint\->new( %params )" .el .SS "\f(CW$merge\fP = Async::MergePoint\->new( \f(CW%params\fP )" .IX Subsection "$merge = Async::MergePoint->new( %params )" This function returns a new instance of a \f(CW\*(C`Async::MergePoint\*(C'\fR object. The \&\f(CW%params\fR hash takes the following keys: .IP "needs => \s-1ARRAY\s0" 8 .IX Item "needs => ARRAY" Optional. An array containing unique item names to wait on. The order of this array is not significant. .IP "on_finished => \s-1CODE\s0" 8 .IX Item "on_finished => CODE" Optional. \s-1CODE\s0 reference to the continuation for when the merge point becomes ready. If provided, will be passed to the \f(CW\*(C`close\*(C'\fR method. .SH "METHODS" .IX Header "METHODS" .ie n .SS "$merge\->close( %params )" .el .SS "\f(CW$merge\fP\->close( \f(CW%params\fP )" .IX Subsection "$merge->close( %params )" Allows an \f(CW\*(C`on_finished\*(C'\fR continuation to be set if one was not provided to the constructor. .IP "on_finished => \s-1CODE\s0" 8 .IX Item "on_finished => CODE" \&\s-1CODE\s0 reference to the continuation for when the merge point becomes ready. .PP The \f(CW\*(C`on_finished\*(C'\fR continuation will be called when every key in the \f(CW\*(C`needs\*(C'\fR list has been notified by the \f(CW\*(C`done()\*(C'\fR method. It will be called as .PP .Vb 1 \& $on_finished\->( %items ) .Ve .PP where the \f(CW%items\fR hash will contain the item names that were waited on, and the values passed to the \f(CW\*(C`done()\*(C'\fR method for each one. Note that this is passed as a list, not as a \s-1HASH\s0 reference. .PP While this feature can be used to pass data from the component parts back up into the continuation, it may be more direct to use normal lexical variables instead. This method allows the continuation to be placed after the blocks of code that execute the component parts, so it reads downwards, and may make it more readable. .ie n .SS "$merge\->needs( @keys )" .el .SS "\f(CW$merge\fP\->needs( \f(CW@keys\fP )" .IX Subsection "$merge->needs( @keys )" When called on an open MergePoint (i.e. one that does not yet have an \&\f(CW\*(C`on_finished\*(C'\fR continuation), this method adds extra key names to the set of outstanding names. The order of this list is not significant. .PP This method throws an exception if the MergePoint is already closed. .ie n .SS "$merge\->done( $item, $value )" .el .SS "\f(CW$merge\fP\->done( \f(CW$item\fP, \f(CW$value\fP )" .IX Subsection "$merge->done( $item, $value )" This method informs the merge point that the \f(CW$item\fR is now ready, and passes it a value to store, to be passed into the \f(CW\*(C`on_finished\*(C'\fR continuation. If this call gives the final remaining item being waited for, the \&\f(CW\*(C`on_finished\*(C'\fR continuation is called within it, and the method will not return until it has completed. .SH "EXAMPLES" .IX Header "EXAMPLES" .SS "Asynchronous Plugins" .IX Subsection "Asynchronous Plugins" Consider a program using \f(CW\*(C`Module::Pluggable\*(C'\fR to provide a plugin architecture to respond to events, where sometimes the response to an event may require asynchronous work. A \f(CW\*(C`MergePoint\*(C'\fR object can be used to coordinate the responses from the plugins to this event. .PP .Vb 1 \& my $merge = Async::MergePoint\->new(); \& \& foreach my $plugin ( $self\->plugins ) { \& $plugin\->handle_event( "event", $merge, @args ); \& } \& \& $merge\->close( on_finished => sub { \& my %results = @_; \& print "All plugins have recognised $event\en"; \& } ); .Ve .PP Each plugin that wishes to handle the event can use its own package name, for example, as its unique key name for the MergePoint. A plugin handling the event synchonously could perform something such as: .PP .Vb 7 \& sub handle_event \& { \& my ( $event, $merge, @args ) = @_; \& .... \& $merge\->needs( _\|_PACKAGE_\|_ ); \& $merge\->done( _\|_PACKAGE_\|_ => $result ); \& } .Ve .PP Whereas, to handle the event asynchronously the plugin can instead perform: .PP .Vb 5 \& sub handle_event \& { \& my ( $event, $merge, @args ) = @_; \& .... \& $merge\->needs( _\|_PACKAGE_\|_ ); \& \& sometime_later( sub { \& $merge\->done( _\|_PACKAGE_\|_ => $result ); \& } ); \& } .Ve .SH "AUTHOR" .IX Header "AUTHOR" Paul Evans