NAME

Ekorn::Pipeline::Futures - stages that wait on Futures and promises, for concurrent I/O

SYNOPSIS

use v5.36;
use Mojo::Base -strict, -async_await;
use Mojo::UserAgent;
use Ekorn::Pipeline;
use Ekorn::Pipeline::Futures qw(in_futures);

my $ua = Mojo::UserAgent->new;

# Twenty pages at a time, given in the order the URLs came.
my @pages = Ekorn::Pipeline->new(
    in_futures(20, async sub ($url) {
        my $tx = await $ua->get_p($url);
        return {url => $url, status => $tx->result->code};
    }),
)->(@urls);

DESCRIPTION

Stages for Ekorn::Pipeline that call a block for each value, in the stage, and keep several of what it gives in flight at once, while the stage waits on them: many requests at a time, in one process, for pages, REST calls and asynchronous database drivers. The run itself stays synchronous; the stage runs the loop only while it waits.

A block gives whatever Future::AsyncAwait's await takes: a Future, such as an IO::Async::Future or a Future::Mojo, or a Mojo::Promise, as Mojo::UserAgent's get_p and Mojo's own async subs give it, with nothing between. The stage waits through the methods await itself uses, so it names no event loop and no one class. Anything else the block returns is its results, at once.

Both stages hold values back, so each result says where it came from: a stage after them sees, in "result_original" in Ekorn::Pipeline::Context, the original of the value that result was made from.

FUNCTIONS

Both are exported by default. To name what you import, as the how-to does:

use Ekorn::Pipeline::Futures qw(in_futures);

To keep them out of your namespace:

use Ekorn::Pipeline::Futures ();

in_futures

in_futures($n, $block)

Calls $block for each value, with the value as its argument and in $_, and passes on what it gives, once it's ready, in the order the values came. What it gives is awaited if it's one thing await takes; anything else, several things included, is its results, at once. It takes no options: one given croaks. At most $n are pending, not yet passed on, at once: those in flight, and those that are ready but wait for an earlier one. So one slow value, ordered, leaves the rest idle once $n are pending; "in_futures_as_ready" goes round it.

in_futures_as_ready

in_futures_as_ready($n, $block)

The same, passing each value's results on as soon as they're ready, in the order they're ready.

RULES FOR A BLOCK

  • Read the run's context before the first await. The block is called in the stage, as a map would be, so it sees its value's result_original, result_number and the run's context. Once it has waited, it resumes inside whatever the stage is doing then, and those say where the run has got to.

  • Not inside a running loop. These stages are for programs that run no loop of their own: scripts, batch jobs, scrapers and command-line tools. Don't run them inside a callback of a loop that's already running, such as a Mojolicious app's request handler. Where the stage can tell, it croaks; on IO::Async a nested run may seem to work, but runs the program's other callbacks in the middle of the pipeline.

  • A limit to each host is the client's. $n is the limit overall. Mojo::UserAgent opens a connection for each request it's waiting on, and needs nothing. Net::Async::HTTP opens one connection to a host by default, and pipelines every request on it, so a server answering a connection's requests one at a time answers them one at a time, however many are in flight: give it max_connections_per_host => 0. Even so, its version 0.50 leaves a request waiting a round for a busy connection, as "A note on Net::Async::HTTP" in Ekorn::Pipeline::Futures::Manual::HowTo explains.

FAILURES AND STOPS

A failed Future, or a rejected promise, is thrown in its turn, after the results before it, with its failure as it was; so is an error the block throws before it gives anything. Once the input has run out, though, what's left is given by the stage's flush, which can't give results and then die: a failure met there loses the results taken with it, as any flush's are.

A Ekorn::Pipeline::Stop in the block, before or after an await, stops the run in its turn, with its values, and with every result before it, the flush's included. It's thrown again as an Ekorn::Pipeline::Stop, whatever its class; one that isn't an array carries nothing.

A stop after the stage, or a run ended from outside, such as by "finish" in Ekorn::Pipeline::Iterator, cancels what's in flight where it can be: a Future is cancelled; a Mojo::Promise can't be, and settles, unheeded, when its loop next runs. Once the input has run out, though, the stage's flush waits for everything it holds before it passes any on, so a stop after the stage then cancels nothing.

DIAGNOSTICS

Expected a number in flight of 1 or more

$n wasn't a whole number of 1 or more.

Expected a code reference

The block wasn't code.

Expected no option %s

The stages take no options: for results as they're ready, use "in_futures_as_ready".

Can't wait for a result: its event loop is already running, and a pipeline runs synchronously inside it

The stage was run inside a callback of the loop it would have to run to wait, as in a Mojolicious app's request handler.

Can't wait for a result: it was still waiting after its wait, as no event loop ran for it, or its loop was already running

What the block gave was still waiting after the stage waited on it: it's tied to no loop that can run, such as a plain Future no one will make ready, or its loop was running and couldn't say so.

SEE ALSO

Ekorn::Pipeline::Futures::Manual::HowTo, the user guide, in recipes; Ekorn::Pipeline::Futures::Manual::Reference, what the stages promise, at length; Ekorn::Pipeline, Future, Future::AsyncAwait, IO::Async, Net::Async::HTTP, Mojo::UserAgent, Mojo::Promise

AUTHOR

Iain Campbell <cpanic@cpan.org>

LICENSE

Copyright (c) 2026 by Iain Campbell.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.