NAME

Parallel::Async::Task - task class for Parallel::Async.

METHODS

$task = Parallel::Async::Task->new(\%args)

Creates a new Parallel::Async::Task instance.

    use Parallel::Async::Task;

    # create new task
    my $task = Parallel::Async::Task->new(code => sub {
        my $result = ...; ## do some task
        return $result;
    });

this code is same as

    use Parallel::Async;

    # create new task
    my $task = async {
        my $result = ...; ## do some task
        return $result;
    };

Arguments can be:

  • code

    CodeRef to run on child process. This CodeRef can get arguments from recv or as_anyevent_child or run method arguments.

my @result = $task->recv(@args)

Execute task on child process and wait for receive return value.

    # create new task
    my $task = async {
        my ($x, $y) = @_;
        return $x + $y;
    };

    my $res = $task->recv(10, 20);
    say $res; # 30
my $watcher = $task->as_anyevent_child(@args)

Execute task on child process and receive return value with AnyEvent->child. This feature required AnyEvent.

    # create new task
    my $task = async {
        my ($x, $y) = @_;
        return $x + $y;
    };

    my $watcher; $watcher = $task->as_anyevent_child(sub {
        my ($pid, $status, $res) = @_;
        say $res; ## 30
        undef $watcher;
    }, 10, 20);
my $pid = $task->run(@args)

Execute task on child process.

    # create new task
    my $task = async {
        my ($url) = @_;
        post($url);
    };

    my $pid = $task->run($url);
    wait;
my $pid = $task->daemonize(@args)

Execute task on daemonized process.

    # create new task
    my $task = async {
        my ($url) = @_;
        post($url);
    };

    my $pid = $task->daemonize($url);
my $chain = $task->join($task1, ...);

Join multiple tasks. Can be execute tasks in parallel by chained task. See also Parallel::Async::Chain for more usage.

$task->reset;

Reset the execution status of the task. This feature is useful when you want to re-execute the same task.

    # create new task
    my $task = async {
        my ($x, $y) = @_;
        return $x + $y;
    };

    my $res = $task->recv(10, 20);
    say $res; # 30

    $res = $task->reset->recv(10, 30);
    say $res; # 40
$task->clone;

Clone and reset the execution status of the task. This feature is useful when you want to execute same tasks in parallel.

    # create new task
    my $task = async {
        my ($x, $y) = @_;
        return $x + $y;
    };

    my @res = $task->join(map { $task->clone } 1..9)->recv(10, 30);

AUTHOR

karupanerura <karupa@cpan.org>