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

Thread::Task::Concurrent - Make simple task pocessing simple

SYNOPSIS

use Thread::Task::Concurrent qw(tmsg);

my @data = qw(a b c d e f g h i j k l m n o p q);

my $tq = Thread::Task::Concurrent->new( task => \&task, max_instances => 4, verbose => 1 );
my $result = $tq->enqueue(@data)->start->join->result;

sub task {
    my $char = shift;

    # sleep some time
    my $sleep_time = int rand 10;
    sleep $sleep_time;

    #calculate result
    my $result = "I'm thread " . threads->tid . " and I slept $sleep_time sec. My result was " . $char x 3;

    return $result;
}

DESCRIPTION

If you have input data and you want to pocess it in the same way, Thread::Task::Concurrent gives you an easy to use interface to getthingsdone(TM).

SUBROUTINES

tmsg($string_message)
tmsg($string_message, $tid_to_display)

Spits out the $string_message in the form:

[thread_id] <message>

thread_id is by default threads->tid, but you can also set it artificially via the $tid_to_display variable.

METHODS

Thread::Task::Concurrent->new(%arg)
task => sub { ... }

Set the subroutine for the task. Example:

sub {
    my ($item, $task_arg) = @_;
    
    return $result_item;
}
arg => $task_arg

Add an additional arg hash/array/scalar to the task/subroutine call.

max_instances => 4

Set the maximum number of threads. Default is 4.

verbose => 0

Switch on/off verbose reporting.

$ttc = $ttc->start()

Start processing.

$ttc = $ttc->join()

Wait for processing end.

$ttc = $ttc->enqueue(@data)

Enqueue items.

$ttc = $ttc->result()

Gather the result.

ACCESSORS

$q = $ttc->queue
$rq = $ttc->result_queue
$task_code_ref = $ttc->task
$task_arg = $ttc->arg
$num = $ttc->max_instances
$threads = $ttc->threads
$is_verbose = $ttc->verbose
$is_finished = $ttc->finished

SEE ALSO

-

AUTHOR

jw bargsten, <cpan at bargsten dot org>