NAME

Parallel::TaskExecutor::Tasks

SYNOPSIS

A simple task (or promise) class for the Parallel::TaskExecutor package.

my $executor = Parallel::TaskExecutor->new();
my $task = $executor->run(sub { return 'foo' });
$task->wait();
is($task->data(), 'foo');

DESCRIPTION

The tasks that this class exposes are lightweight promises that can be used to wait for the end of the parallel processing and read the result of that processing.

METHODS

constructor

The constructor of this class is private, it can only be built by Parallel::TaskExecutor through a call to run().

destructor

The destructor of a Parallel::TaskExecutor::Task object will block until the task is done if you no longer keep a reference to its parent Parallel::TaskExecutor object. If the parent executor is still live, then that object will be responsible to wait for the end of the task (either through an explicit call to wait() or in its destructor).

wait

$task->wait();

Blocks until the task is done. When this function returns, it is guaranteed that the task is in the done state (that is, that done() will return true).

Returns a true value if the child task succeeded. If the task failed and catch_error was set in the parent executor when the task started, then this method will return a false value.

data

my @data = $task->data();
my $data = $task->data();

Returns the result value of a finished task (produced by the code-reference that was passed to the run() call of the executor). If called in list context, returns all the produced value. If called in scalar context, returns only the first value. Note that the code-reference itself in the task has been called in a list context by default, unless the scalar option was passed to its executor.

It is an error to call this method on a task that is still running. So you must be sure that the task is done before you call it (either through a call to wait() or to done() for example). See also the get() method which combines a call to wait() and to data().

If the task failed and catch_error was set in the parent executor when the task started, then this method will die() with the child task error.

running

print "Still running\n" if $task->running();

Returns whether the task is still running.

done

print "Done\n" if $task->done();

Returns whether the task is done. This is guaranteed to always be the opposite of done().

get

my $data = $task->get();

Waits until the task is done and returns the result of the task. See the documentation of the wait() and data() methods for more details, in particular regarding scalar and list context data.

signal

$task->signal('HUP');

Sends the given signal to the task. Signal can be anything accepted by the kill() method, so either a signal name or a signal number. See kill() for how to get the list of supported signals.

Note that even if the signal kills the task you should still in general wait() for it at some point. Also, unless the task gracefully handles the signal, you will probably need to pass the catch_error option to the run() call when the task is started, otherwise your whole program will be aborted.

kill

$task->kill();

This is a synonym of signal()|/signal> but where the default argument is SIGKILL. You can still pass a different signal name if you want.