The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

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.