The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Argon::Client

SYNOPSIS

    use Argon::Client;

    # Connect
    my $client = Argon::Client->new(host => '...', port => XXXX);

    # Send task and wait for result
    my $the_answer = $client->queue(sub {
        my ($x, $y) = @_;
        return $x * $y;
    }, [6, 7]);

    # Send task and get a deferred result that can be synchronized later
    my $deferred = $client->defer(sub {
        my ($x, $y) = @_;
        return $x * $y;
    }, [6, 7]);

    my $result = $deferred->();

    # Close the client connection
    $client->shutdown;

DESCRIPTION

Establishes a connection to an Argon network and provides methods for executing tasks and collecting the results.

METHODS

new(host => $host, port => $port)

Creates a new Argon::Client. The connection is made lazily when the first call to "queue" or "connect" is performed. The connection can be forced by calling "connect".

connect

Connects to the remote host.

queue($f, $args)

Sends a task to the Argon network to evaluate $f-(@$args)> and returns the result. Since Argon uses Coro, this method does not actually block until the result is received. Instead, it yields execution priority to other threads until the result is available.

If an error occurs in the execution of $f, an error is thrown.

defer($f, $args)

Similar to "queue", but instead of waiting for the result, returns an anonymous function that, when called, waits and returns the result. If an error occurs when calling <$f>, it is re-thrown from the anonymous function.

shutdown

Disconnects from the Argon network.

A NOTE ABOUT SCOPE

Storable is used to serialize code that is sent to the Argon network. This means that the code sent will not have access to variables and modules outside of itself when executed. Therefore, the following will not work:

    my $x = 0;
    $client->queue(sub { return $x + 1 }); # $x not found!

The right way is to pass it to the function as part of the task's arguments:

    my $x = 0;

    $client->queue(sub {
        my $x = shift;
        return $x + 1;
    }, [$x]);

Similarly, module imports are not available to the function:

    use Data::Dumper;

    my $data = [1,2,3];
    my $string = $client->queue(sub {
        my $data = shift;
        return Dumper($data); # Dumper not found
    }, [$data]);

The right way is to import the module inside the task:

    my $data = [1,2,3];
    my $string = $client->queue(sub {
        use Data::Dumper;
        my $data = shift;
        return Dumper($data);
    }, [$data]);

AUTHOR

Jeff Ober <jeffober@gmail.com>