NAME

Parallel::SubFork - Manage Perl functions in forked processes.

SYNOPSIS

        use Parallel::SubFork;
        my $manager = Parallel::SubFork->new();
        
        # Start two parallel tasks
        $manager->start(sub { sleep 10; print "Done\n" });
        $manager->start(\&callback, @args);
        
        # Wait for all tasks to resume
        $manager->wait_for_all();
        
        # Loop through all tasks
        foreach my $task ($manager->tasks) {
                # Access any of the properties
                printf "Task with PID %d resumed\n", $task->pid;
                printf "Exist status: %d, exit code: %d\n", $task->status, $task->exit_code;
                printf "Args of task where: %s\n", join(', ', $task->args);
                print "\n";
        }

or more easily:

        use Parallel::SubFork qw(sub_fork);
        
        my $task = sub_fork(\&callback, @args);
        $task->wait_for();

DESCRIPTION

This module provides a simple wrapper over the module Parallel::SubFork::Task which in turns simplifies the usage of the system calls fork and waitpid. The idea is to isolate the tasks to be execute in functions or closures and to execute them in a separated process in order to take advantage of parallelization.

TASKS

A task is simply a Perl function or a closure that will get executed in a different process. This module will take care of creating and managing the new processes. All that's left is to code the logic of each task and to provide the proper inter process communication (IPC) mechanism if needed.

A task will run in it's own process thus it's important to understand that all modifications to variables within the function, even global variables, will have no impact on the parent process. Communication or data exchange between the task and the dispatcher (the code that started the task) has to be performed through standard IPC mechanisms. For further details on how to establish different communication channels refer to the documentation of perlipc.

Since a task is running within a process it's expected that the task will return an exit code (0 for an execution without flaws and any other integer for reporting an error) and not a true value in the Perl sense. The return value will be used as the exit code of the process that's running the task.

FUNCTIONS

The module provides the following functions:

sub_fork

This function provides a simple way for creating and launching tasks. It is declared using a prototype which allows it to be called as:

        my $task = sub_fork { print "$$ > $_\n" for 1 .. 10 };
        $task->wait_for();

Parameters:

$code

The code reference to execute.

@args (optional)

The arguments to pass to the code reference.

METHODS

The module defines the following methods:

new

Creates a new Parallel::SubFork.

start

Starts the execution of a new task in a different process. A task consists of a code reference (a closure or a reference to a subroutine) and of an arguments list.

This method will actually fork a new process and execute the given code reference in the child process. For the parent process this method will return automatically. The child process will start executing the code reference with the given arguments.

The parent process, the one that started the task should wait for the child process to resume. This can be performed individually on each tasks through the method "wait_for"" in "Parallel::SubFork::Task or for all tasks launched through this instance through the method "wait_for_all"

NOTE: This method requires that the caller process is the same process as the one that created the instance object being called.

Parameters:

$code

The code reference to execute.

@args (optional)

The arguments to pass to the code reference.

wait_for_all

This method waits for all tasks started so far and returns when they all have resumed. This is useful for creating a rally point for multiple tasks.

NOTE: This method requires that the caller process is the same process as the one that created the instance object being called.

tasks

Returns the tasks started so far by this instance. This method returns a list and not an array ref.

_assert_is_dispatcher

Used to check if the current process is the same one that invoked the constructor.

This is required as only the dispatcher process is allowed to start and wait for tasks.

NOTES

The API is not yet frozen and could change as the module goes public.

SEE ALSO

Take a look at POE for asynchronous multitasking and networking.

AUTHOR

Emmanuel Rodriguez, <emmanuel.rodriguez@gmail.com>

COPYRIGHT AND LICENSE

Copyright (C) 2008-2010 by Emmanuel Rodriguez

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.8 or, at your option, any later version of Perl 5 you may have available.