geopal-solutions/gnu-parallel-wrapper

GNU Parallel二进制文件的包装库

dev-master 2014-02-28 10:26 UTC

This package is auto-updated.

Last update: 2024-09-29 03:41:39 UTC


README

Simple PHP wrapper class for the GNU Parallel tool. Allows for running single threaded tasks in parallel on one or multiple machines, from within PHP.

此包需要预先安装GNU Parallel!

如果您在运行PHP脚本所在的机器上没有安装Parallel,将会抛出InvalidBinaryException异常。(参见示例。)

示例1

/**
 *
 * Running commands on the local host
 *
 */

use Parallel\Exceptions\InvalidBinaryException;
use Parallel\Wrapper;

// You can initialize the Wrapper with or without parameters
$parallel = new Wrapper();

try {
    // Set path to binary
    $parallel->initBinary('/path/to/parallel/binary');

    // Add the commands you want to run in parallel
    $parallel->addCommand('/path/to/command/one.sh');
    $parallel->addCommand('/path/to/command/two.sh');
    $parallel->addCommand('/path/to/command/three.sh');

    /**
     * Setting the parallelism to 0 or "auto" will
     * result in a parallelism setting equal to the
     * number of commands you whish to run
     *
     * Use the maxParallelism setting to set a cap
     */
    $parallel->setParallelism('auto');
    $parallel->setMaxParallelism(10);

    // Run the commands and catch the output from the console
    $output = $parallel->run();
} catch (InvalidBinaryException $exception) {
    // The binary file does not exist, or is not executable
}

示例2

/**
 *
 * Running commands on multiple hosts
 *
 */

use Parallel\Exceptions\InvalidBinaryException;
use Parallel\Wrapper;

$commands = array(
    '/path/to/command/one.sh',
    '/path/to/command/two.sh',
    '/path/to/command/three.sh'
);

$maxParallelism = 10;

// You can initialize the Wrapper with or without parameters
$parallel = new Wrapper('/path/to/parallel/binary', $commands, $maxParallelism);

try {
    /**
     * You can still set the parallelism manually, or leave it
     * to the wrapper to calculate it
     */
    $parallel->setParallelism(8);

    $parallel->addServer('foo@example1.com');
    $parallel->addServer('bar@example2.com');
    $parallel->addServer('baz@example3.com');

    /**
     * By default, the local host is also included
     * in the list of servers used for execution.
     * 
     * You can exclude it from the list by setting
     * the remoteOnly flag to true 
     */
    $parallel->useRemoteOnly(true);

    // Run the commands and catch the output from the console
    $output = $parallel->run();
} catch (InvalidBinaryException $exception) {
    // The binary file does not exist, or is not executable
}