Acme-Parataxis
view release on metacpan or search on metacpan
lib/Acme/Parataxis/Nursery.pod view on Meta::CPAN
=pod
=encoding utf8
=head1 NAME
Acme::Parataxis::Nursery - Structured Concurrency Container for Scoped Fiber Lifecycles
=head1 SYNOPSIS
use v5.40;
use Acme::Parataxis qw[async nursery];
async {
my ( $user, $preferences );
my $ok = eval {
nursery( sub ($n) {
$n->spawn( sub { $user = fetch_user(123) } );
$n->spawn( sub { $preferences = fetch_prefs(123) } );
} );
1;
};
die $@ unless $ok;
# Both children are guaranteed to have completed:
say "User: " . $user->name;
};
=head1 DESCRIPTION
C<Acme::Parataxis::Nursery> implements structured concurrency (popularized by Trio and Go errgroups). When concurrent
tasks are executed inside a nursery, the lifetimes of all child fibers are bounded by the enclosing block:
=over
=item *
B<Guaranteed join>: C<nursery( sub ($n) { ... } )> does not return until every child spawned via C<< $n->spawn >> has
completely terminated.
=item *
B<Prompt sibling cancellation>: If any child fiber dies with an unhandled exception, the nursery immediately cancels
all surviving siblings via an internal cancellation token. The nursery drains all in-flight work before rethrowing the
culprit failure.
=item *
B<No orphaned fibers>: Even if the parent fiber is interrupted (e.g. by an outer C<with_timeout>), all children in the
nursery are cancelled and drained before the parent unwinds.
=back
Fibers spawned independently via the top-level C<fiber { ... }> or C<spawn()> inside the block are B<not> enrolled in
the nursery and do not delay its join.
=head1 METHODS
=head2 C<spawn( sub { ... } )>
my $child = $n->spawn( sub {
do_concurrent_work();
( run in 0.805 second using v1.01-cache-2.11-cpan-234fa220139 )