[![Actions Status](https://github.com/nukopian/Ekorn-Pipeline-Futures/actions/workflows/test.yml/badge.svg?branch=main)](https://github.com/nukopian/Ekorn-Pipeline-Futures/actions?workflow=test)
[![CPAN](https://img.shields.io/cpan/v/Ekorn-Pipeline-Futures)](https://metacpan.org/release/Ekorn-Pipeline-Futures) [![Kwalitee](https://cpants.cpanauthors.org/dist/Ekorn-Pipeline-Futures.svg)](https://cpants.cpanauthors.org/dist/Ekorn-Pipeline-Futures)

# Ekorn-Pipeline-Futures

Stages for [Ekorn::Pipeline](https://metacpan.org/pod/Ekorn%3A%3APipeline) that wait on Futures and promises: many
requests at once, in one process, beside the call.

A function that takes stages from its caller, as [Ekorn::Pipeline](https://metacpan.org/pod/Ekorn%3A%3APipeline)'s
do, runs each value it hands out through them, one at a time. When what
a caller wants done with each value is to wait on the network, for a
page, a REST call or a query, one at a time is slow. `in_futures` lets
the caller keep many in flight, with nothing of the function changed:

```perl
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;
my @urls = map {"https://example.com/page/$_"} 1 .. 100;

# 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);
```

## This, Workers, or neither?

Ekorn::Pipeline has two distributions for doing many things at once.
Which fits depends on what each value's work waits for:

- **Waiting on I/O a library can do asynchronously,** giving
Futures or promises, as [Mojo::UserAgent](https://metacpan.org/pod/Mojo%3A%3AUserAgent) and [IO::Async](https://metacpan.org/pod/IO%3A%3AAsync) do: this
one. One process waits on many requests at once.
- **Work that blocks,** as the DBI's does, or a module's with only
a synchronous interface; or **work for the CPU**:
[Ekorn::Pipeline::Workers](https://metacpan.org/pod/Ekorn%3A%3APipeline%3A%3AWorkers). Each value's work runs in a worker, a perl
of its own, so it's a function in a module.
- **Neither,** as each value's work takes microseconds: an
ordinary stage.

## What it does

- **Any loop `await` knows.** A block gives a [Future](https://metacpan.org/pod/Future), such as
IO::Async's, or a [Mojo::Promise](https://metacpan.org/pod/Mojo%3A%3APromise), as [Mojo::UserAgent](https://metacpan.org/pod/Mojo%3A%3AUserAgent)'s `get_p`
does: whatever [Future::AsyncAwait](https://metacpan.org/pod/Future%3A%3AAsyncAwait)'s `await` takes. The stage names
no loop.
- **In order, or as they're ready.** `in_futures` passes results
on in the order the values came; `in_futures_as_ready`, as each is.
- **Where each result came from.** The stages after it see each
result's own original, as Ekorn::Pipeline v0.5.0 lets a stage that
holds values back say.
- **Failures and stops in their turn,** and a stop after it
cancels what's in flight, where it can be: a Future is cancelled, and a
[Mojo::Promise](https://metacpan.org/pod/Mojo%3A%3APromise), which can't be, is left to settle.
- **On Windows too.** Nothing forks; the tests, and every recipe
in the user guide, run on Linux, macOS and Windows.

## What it isn't for

It's for programs that run no loop of their own: scripts, batch jobs,
scrapers and command-line tools. A pipeline's run is synchronous, so
don't run these stages inside a callback of a loop that's already
running, such as a Mojolicious app's request handler.

## Documentation

[Ekorn::Pipeline::Futures::Manual::HowTo](https://metacpan.org/pod/Ekorn%3A%3APipeline%3A%3AFutures%3A%3AManual%3A%3AHowTo), the user guide, is the
place to start: recipes, each a whole program, tested as it stands.
[Ekorn::Pipeline::Futures::Manual::Reference](https://metacpan.org/pod/Ekorn%3A%3APipeline%3A%3AFutures%3A%3AManual%3A%3AReference) says what the stages
promise, at length, each promise linked to the recipes that show it.
[Ekorn::Pipeline::Futures](https://metacpan.org/pod/Ekorn%3A%3APipeline%3A%3AFutures) says the same briefly, and is the
authority where they differ.

## Installing

```
cpanm Ekorn::Pipeline::Futures
```

It needs Perl 5.36 or later, [Ekorn::Pipeline](https://metacpan.org/pod/Ekorn%3A%3APipeline) v0.5.x and [Future](https://metacpan.org/pod/Future).
The loops and clients a block uses are the caller's to install.

## Licence

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.

## Acknowledgements

Designed and created by Iain Campbell, in collaboration with Claude,
Anthropic's AI model.
