NAME

Parallel::Performing - Perl extension for the safe performance of the tasks in the queue.

SYNOPSIS

use Parallel::Performing;

$handler = Parallel::Performing->new;

DESCRIPTION

Parallel::Performing is a module for Perl programs that can safely perform the tasks in the queue.

Features: * Tasks are performed at the same time in the flow depending on kid_max. * For each task you can set timeout. * All tasks consist in the loop. So you can easy exit from queue or kill all tasks, or add new task in the queue without any interrupts. =head1 METHODS

Tasks in the queue

new()

Create a new Parallel::Performing

my $handler = Parallel::Performing->new( %settings );

where settings are:

kid_max

Maximum number of kids

$settings{'kid_max'} = 5; # default
timeout

Timeout for single task (in seconds)

$settings{'timeout'} = 5; # default 
kid_task

Argument for this parameter is a function which handles the task and passes the result to kid_result_task

$settings{'kid_task'} = sub { };
kid_result_task

Argument for this parameter is a function which handles the result. Here you can save it in ather variables.

$settings{'kid_result_task'} = sub { };
kid_timeout_task

Argument for this parameter is a function which run if was timeout.

$settings{'kid_timeout_task'} = sub { };
loop

Argument for this parameter is a function which returns true(1) if we should continue and false(0) if necessary to stop. Here you can specify the possible criteria for stopping.

$settings{'loop'} = sub { };
add_task($key, $task)

Each task should have a key. So when you add a new task, the first parameter should be key and everything else will be its elements.

$handler->add_task($key, $task);
run_queue(%settings)

Run the queue

$handler->run_queue;

Factor finish

is_queue_finished()

Return true(1) if queue is finished (all tasks are done) or false(0)

if ($handler->is_queue_finished) {
	
} else {
	
}
is_loop_finished()

Return true(1) if loop is finished or false(0)

if ($handler->is_loop_finished) {
	
} else {
	
}

EXAMPLES

This example shows how you can use Parallel::Performing for manipulate connections:

use Parallel::Performing;
use LWP::Simple;

$handler = Parallel::Performing->new;

%pages = ();
$handler->add_task('page1', 'http://cpan.org/page1.html');
$handler->add_task('page2', 'http://cpan.org/page2.html');
$handler->add_task('page3', 'http://cpan.org/page3.html');

$handler->run_queue(
  'kid_max' => 3,
  'timeout' => 5, # sec

  'kid_task' => sub {
    ($key, $url) = @_;
    $content = get($url);
    return ($url,$content);
  },

  'kid_result_task' => sub {
    ($key, $url, $content) = @_;
    $pages{$key} = $content;
    return;
  },

  'kid_timeout_task' => sub {
    ($key, $url) = @_;
    print "'$key' is timeouted: ($url)\n";
  },

  'loop' => sub { return 1 },
);


if ($handler->is_queue_finished) {
  while ( ($page,$content) = each %pages ) {
    print "page: $page\n";
    print "content: $content\n\n";
  }
}

SEE ALSO

POSIX, Storable, IO::Select, IO::Pipe

AUTHOR

Alexander Sviridenko, <mail@d2rk.com>

COPYRIGHT AND LICENSE

Copyright (C) 2008 by Alexander Sviridenko

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

2 POD Errors

The following errors were encountered while parsing the POD:

Around line 351:

You forgot a '=back' before '=head2'

Around line 423:

=back without =over