NAME

Ekorn::Pipeline::Workers - stages that run each value in a worker process

SYNOPSIS

# In My/Geo.pm, which each worker loads:
package My::Geo;
use v5.36;
use DBI;

sub connect_db ($dsn)         { return DBI->connect($dsn, '', '', {RaiseError => 1}) }
sub lookup  ($postcode, $dbh) { return $dbh->selectrow_hashref(
    'SELECT lat, lon FROM postcodes WHERE postcode = ?', undef, $postcode) }
sub disconnect ($dbh)         { $dbh->disconnect }

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

# Each distinct postcode looked up once, in 4 workers, each row
# joined with its postcode's result, in the order the rows came.
my @rows = Ekorn::Pipeline->new(
    once_per(sub { $_->{postcode} }, 4, 'My::Geo::lookup',
        init      => 'My::Geo::connect_db',
        init_args => [$dsn],
        done      => 'My::Geo::disconnect',
        join      => sub ($row, $geo) { +{%$row, %$geo} }),
)->iterator($source)->all;

DESCRIPTION

Stages for Ekorn::Pipeline that run a function for each value in a pool of worker processes, for blocking libraries, such as the DBI, and work for the CPU, beside the call, with nothing of the function that takes the stages changed.

Each worker is a new perl, started fresh, never a clone of the program, on Unix and on Windows alike, and talks to the program on a loopback socket of its own. So a worker inherits nothing the program holds, a database handle included, and nothing it does is undone as it leaves. For the same reason the function a worker runs is one a new perl can load: its name, as 'Module::function', or a code reference to a named sub in a module, as \&Module::function, which gives its name; never an anonymous sub, which a new perl can't reach, nor a sub of the program's own script, in any package, which a worker doesn't load. A worker loads a function's module by its package, as My/Module.pm for My::Module::function, so the package must be the module's own, not a second package in another's file. Each is checked as the stage is made. So are "init" and "done". Each worker loads the module afresh, so it sees it as it is when loaded: what the program changed in it at run time, such as a package variable, isn't in the worker. A worker is given the program's @INC, without its hooks, the code references in it, and its environment, as it is when the workers start.

Values and results cross by Storable: plain data, and objects whose class the worker can load or make. What can't be frozen, such as code, croaks, naming the value.

The 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

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

use Ekorn::Pipeline::Workers qw(in_workers);

To keep them out of your namespace:

use Ekorn::Pipeline::Workers ();

in_workers

in_workers($n, 'Module::function', %options)
in_workers($n, \&Module::function, %options)

Runs the function for each value, in one of $n workers, given the value, in $_ and as its argument, and what "init" returned after it, and passes its results on in the order the values came. At most window values, by default 2 * $n, are sent and not yet passed on. A function may give no results, or several, as a stage may.

In the worker, the function sees where its value stands in the run, as a stage would: "run_context" in Ekorn::Pipeline, "ending" in Ekorn::Pipeline, and Ekorn::Pipeline::Context's result_number and result_original. A context or an original that can't be frozen is left behind, and it sees undef for both.

in_workers_as_ready

in_workers_as_ready($n, 'Module::function', %options)
in_workers_as_ready($n, \&Module::function, %options)

The same, passing each value's results on as they're back, so one slow value holds up none of the rest.

once_per

once_per($key, $n, 'Module::function', %options)
once_per($key, $n, \&Module::function, %options)

Runs the function once for each distinct key, which $key, a code reference called in the program with the value, gives; and passes each value on as join makes it, from the value and its key's results, in the program. Without a join, each value becomes its key's results. A key asked for while it's being looked up waits for that lookup. Keys are compared as strings, and kept, with their results, for the run. Only the key crosses to a worker, so a value never does; the function is given it as its value, and sees where the first value to ask for it stands. Its options are in_workers' and:

join

A code reference, called in the program, given the value, in $_ and as its first argument, and its key's results after it.

hold

How many values may wait behind keys not yet back before the stage waits, by default 1000.

ordered
ordered => 0

Each value passed on as soon as its key's results are back, rather than in the order the values came, which is the default.

OPTIONS

init
init => 'Module::function'
init => \&Module::function

Run once in each worker as it starts, before the first value is sent, given "init_args". What it returns follows the value in each call of the function, and is given to "done". A failed init is the run's error.

init_args
init_args => [@args]

An array reference of what init is given. It crosses by Storable: a DSN, say, never a handle. Without an init, it croaks.

done
done => 'Module::function'
done => \&Module::function

Run in each worker as it leaves, once the run is done, given what init returned. A worker still working after a stop or an error is ended without it.

window

How many values may be sent and not yet passed on, by default 2 * $n, and at least $n; for once_per, how many keys may be looked up at once.

An option not listed for the stage croaks, as it's made.

WHEN WORKERS START AND END

The workers start as a run's first value reaches the stage, so a run with no values starts none, and they end with the run: there's a pool for each run. Starting a perl isn't free: with the stage's modules alone, about 13 ms a worker on macOS, and more on Windows, where 4 workers took a quarter to half a second. So these stages pay for work of milliseconds a value over many values, not for short runs.

What the program made is in no worker: a class made at run time, say, must be made in the worker, by init, or as an object thaws.

FAILURES AND STOPS

An error in the function is thrown in its turn, after the results before it, as it was: an object too, such as an exception class's, if Storable can freeze it, or its message if not, as when it holds a handle. Its class must be one the program loads, for its methods. A Ekorn::Pipeline::Stop stops the run in its turn, with its values, and with every result before it, the flush's included; it's thrown again in the program as an Ekorn::Pipeline::Stop, whatever its class, and its values aren't given to join. A worker that exits without replying is an error naming the value it had. A stop after the stage ends the workers: an idle one runs "done" and leaves, one still working is killed. Once the values have run out, though, the stage's flush finishes every value it holds before it passes any on, so a stop after the stage then ends no work early.

An error met in the stage's flush, at the end of a run, loses the results the flush took before it, as a flush can't pass values on and then die.

DIAGNOSTICS

Expected a number of workers of 1 or more
Expected no option %s

The stage doesn't take that option: a misspelling, say, or ordered, which only once_per takes.

Expected window to be a whole number
Expected hold to be a whole number
Expected a window at least the number of workers
Expected %s to be a named sub in a module, not an anonymous one: a worker is a new perl, which it can't reach

The function, init or done, as the message names it, was a code reference to an anonymous sub.

Expected %s to be a sub in a module, not the program's %s: a worker is a new perl, which loads modules, not the program

It was a sub of the program's script, given by name or by code reference, in main or in a package of the script's own.

Expected %s to be in a module of its own name, %s, which a worker can load

It was a sub in a second package of a module's file, such as My::Two::Inner in My/Two.pm: a worker would look for My/Two/Inner.pm.

Expected %s to be given by name, as 'Module::function', or as a code reference to one: a worker is a new perl, which a closure in this one can't reach

It was neither a name nor a code reference.

Expected %s to exist, but there's no %s

Its module loaded, or was already, without the function.

Expected %s's module, %s, to load: %s

The module wouldn't load, for the reason Perl gave: not found in @INC, say, or not compiling. The program loads it too, as the stage is made, to check the name.

Expected init_args to be an array reference
Expected init with init_args

init_args was given without an init to give them to.

Expected a code reference for the key
Expected a code reference to join
Expected a key for each value, not undef

The messages below come from a run. Each starts with the name of the stage that gave it, in_workers, in_workers_as_ready or once_per; and where one names a value, once_per's names a key, as a key is what it sends.

%s can't send %s #%s to a worker: %s

The value couldn't be frozen. Values are counted from 1, in the order they reached the stage; for once_per, the keys it sent are.

%s can't send back the results of %s #%s: %s

The function's results couldn't be frozen.

%s lost worker %s, which exited, status %s, with %s #%s
%s couldn't set up a worker: %s

init failed, or the function, init or done couldn't be found in the worker.

%s waited 30s for its workers to start
%s can't listen for its workers: %s
%s couldn't start a worker: %s
%s can't send its init_args to a worker: %s
%s lost worker %s: %s

SEE ALSO

Ekorn::Pipeline::Workers::Manual::HowTo, the user guide, in recipes; Ekorn::Pipeline::Workers::Manual::Reference, what the stages promise, at length; Ekorn::Pipeline, Storable

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.