$ cat -n fork5.pl
1 #!/usr/bin/perl -w
2 use strict;
3 use GRID::Machine;
4 use Data::Dumper;
5
6 my $host = $ENV{GRID_REMOTE_MACHINE};
7 my $machine = GRID::Machine->new( host => $host );
8
9 my $p = $machine->fork( q{
10
11 print "stdout: Hello from process $$. args = (@_)\n";
12 print STDERR "stderr: Hello from process $$\n";
13
14 use List::Util qw{sum};
15 return { s => sum(@_), args => [ @_ ] };
16 },
17 args => [ 1..4 ],
18 );
19
20 # GRID::Machine::Process objects are overloaded
21 print "Doing something while $p is still alive ...\n" if $p;
22
23 my $r = $machine->waitpid($p);
24
25 print "Result from process '$p': ",Dumper($r),"\n";
26 print "GRID::Machine::Process::Result objects are overloaded i
+n a string context:\n$r\n";
When executed, the former program produces an output similar to this:
$ perl fork5.pl
Doing something while 5220:5230:some.machine:5234:5237 is still aliv
+e ...
Result from process '5220:5230:some.machine:5234:5237': $VAR1 = bles
+s( {
'machineID' => 0,
'stderr' => 'stderr: Hello from process 5237
',
'descriptor' => 'some.machine:5234:5237',
'status' => 0,
'waitpid' => 5237,
'errmsg' => '',
'stdout' => 'stdout: Hello from process 5237. args
+= (1 2 3 4)
',
'results' => [ { 'args' => [ 1, 2, 3, 4 ], 's' => 1
+0 } ]
}, 'GRID::Machine::Process::Result' );
GRID::Machine::Process::Result objects are overloaded in a string co
+ntext:
stdout: Hello from process 5237. args = (1 2 3 4)
stderr: Hello from process 5237
The fork method returns a GRID::Machine::Process object. The first argument must be a string containing the code that will be executed by the forked process in the remote machine. Such code is always called in a list context. |