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

# Ekorn-Pipeline-Workers

Stages for [Ekorn::Pipeline](https://metacpan.org/pod/Ekorn%3A%3APipeline) that run each value in a worker process:
blocking work, beside the call, in parallel, on Unix and Windows alike.

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 blocks, a query through the DBI, a
call to a library that waits, or work for the CPU, one at a time is
slow. `in_workers` runs it in a pool of workers, with nothing of the
function changed:

```perl
# In My/Thumbs.pm, which each worker loads:
package My::Thumbs;
use v5.36;
sub make ($path) { ...; return "$path.thumb.png" }

# In the program:
use Ekorn::Pipeline;
use Ekorn::Pipeline::Workers qw(in_workers);

my @paths  = glob 'photos/*.jpg';
my @thumbs = Ekorn::Pipeline->new(
    in_workers(8, 'My::Thumbs::make'),
)->(@paths);
```

## This, Futures, 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:
[Ekorn::Pipeline::Futures](https://metacpan.org/pod/Ekorn%3A%3APipeline%3A%3AFutures). 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**: this one. 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

- **Fresh perls, never clones.** Each worker is a new perl, on a
socket of its own, so it inherits nothing the program holds, a
database handle included, and works the same on Windows as on Unix.
- **Named functions.** As a worker is a new perl, what it runs is
a function in a module, named as `'Module::function'` or given as
`\&Module::function`, never an anonymous sub. `init` sets a worker
up once, given `init_args`, such as a DSN to connect from; `done`
tears it down.
- **In order, or as they're back.** `in_workers` passes results
on in the order the values came; `in_workers_as_ready` as each is back;
`once_per` runs the function once a key, and joins each value with
its key's results.
- **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.** A worker that dies is an
error naming the value it had; a stop after it ends the workers.

## What it isn't for

Starting a perl isn't free: about 13 milliseconds a worker on macOS,
and a quarter to half a second for 4 workers on Windows. So these
stages pay for work of milliseconds a value over many values, not for
short runs.

## Documentation

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

## Installing

```
cpanm Ekorn::Pipeline::Workers
```

It needs Perl 5.36 or later and [Ekorn::Pipeline](https://metacpan.org/pod/Ekorn%3A%3APipeline) v0.5.x, and nothing
else beyond Perl's own modules.

## 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.
