Multi

Async::Command::Multi

Executes multiple Async::Command instances.

Synopsis

    use Async::Command::Multi;

    my %command;
    %command<ngroups_max> = </bin/cat /proc/sys/kernel/ngroups_max>;
    %command<uptime>      = </usr/bin/uptime>;
    ...
    %command<commandN>    = </bin/commandN --cN>;

    my $command-manager = Async::Command::Multi.new(:%command, :2time-out, :4batch);
    $command-manager.sow;                   # start promises

    # do other things...

    my %result = $command-manager.reap;     # await promises

    # examine $*OUT from each successfully Kept promise
    for %result.keys -> $key {
        printf("[%s] %s:\n", !%result{$key}.exit-code ?? '+' !! '-', $key);
        .say for %result{$key}.stdout-results;
    }

Methods

new()

:%command

keys are arbitrary and respected by Async::Command to maintain associations.

values are independent commands to execute. Absolute paths are encouraged.

:$time-out

Optional global timer for each promise, in Real seconds. No individual promise should take longer than this number of Real seconds to complete its thread. '0' indicates no time out.

:$batch

Promise throttle. Default = 16. Mutable for subsequent re-runs.

:$attempts

Optional retry attempts maximum.

:$delay

Optional delay interval between retry attempts.

sow()

Method sow() starts multiple Async::Command instances (promises).

reap()

Method reap() awaits all sown promises and returns a hash of Async::Command::Result objects.

Example

Given

    #!/usr/bin/env raku
    use Async::Command::Multi;
    my %command;
    %command<ctools> = <ssh ctools uname -n>;
    %command<jtools> = <ssh jtools notarealcommand>;
    dd $ = Async::Command::Multi.new(:%command, :1time-out).sow.reap;

Output

Hash $ = ${
    :ctools(Async::Command::Result.new(
        command => ["ssh", "ctools", "uname", "-n"],
        exit-code => 0,
        stdout-results => "CTUNIXVMADMINPv\n",
        stderr-results => "",
        time-out => 1,
        timed-out => Bool::False,
        unique-id => "ctools")),
    :jtools(Async::Command::Result.new(
        command => ["ssh", "jtools", "notarealcommand"],
        exit-code => 127,
        stdout-results => "",
        stderr-results => "sh: notarealcommand: command not found\n",
        time-out => 1,
        timed-out => Bool::False,
        unique-id => "jtools"))
}

Async::Command v0.2.1

Async::Command - Run a command as a thread; Async::Command::Multi - do lots in parallel

Authors

  • Mark Devine

License

Artistic-2.0

Dependencies

Test Dependencies

Provides

  • Async::Command
  • Async::Command::Multi
  • Async::Command::Result

The Camelia image is copyright 2009 by Larry Wall. "Raku" is trademark of the Yet Another Society. All rights reserved.