… | |
… | |
8 | |
8 | |
9 | use AnyEvent; |
9 | use AnyEvent; |
10 | use AnyEvent::Fork::Remote; |
10 | use AnyEvent::Fork::Remote; |
11 | |
11 | |
12 | my $rpc = AnyEvent::Fork::Remote |
12 | my $rpc = AnyEvent::Fork::Remote |
13 | ->new |
13 | ->new_execp ("ssh", "ssh", "othermachine", "perl") |
14 | ->require ("MyModule") |
14 | ->require ("MyModule") |
15 | ->run ("MyModule::run", my $cv = AE::cv); |
15 | ->run ("MyModule::run", my $cv = AE::cv); |
16 | |
16 | |
17 | my $fh = $cv->recv; |
17 | my $fh = $cv->recv; |
18 | |
18 | |
… | |
… | |
43 | |
43 | |
44 | =item * C<fork> does not actually fork, but will create a new process |
44 | =item * C<fork> does not actually fork, but will create a new process |
45 | |
45 | |
46 | =back |
46 | =back |
47 | |
47 | |
48 | =head1 EXAMPLES |
48 | =head1 EXAMPLE |
|
|
49 | |
|
|
50 | This example uses a local perl (because that is likely going to work |
|
|
51 | without further setup) and the L<AnyEvent::Fork::RPC> to create simple |
|
|
52 | worker process. |
|
|
53 | |
|
|
54 | First load the modules we are going to use: |
|
|
55 | |
|
|
56 | use AnyEvent; |
|
|
57 | use AnyEvent::Fork::Remote; |
|
|
58 | use AnyEvent::Fork::RPC; |
|
|
59 | |
|
|
60 | Then create, configure and run the process: |
|
|
61 | |
|
|
62 | my $rpc = AnyEvent::Fork::Remote |
|
|
63 | ->new_execp ("perl", "perl") |
|
|
64 | ->eval (' |
|
|
65 | sub myrun { |
|
|
66 | "this is process $$, and you passed <@_>" |
|
|
67 | } |
|
|
68 | ') |
|
|
69 | ->AnyEvent::Fork::RPC::run ("myrun"); |
|
|
70 | |
|
|
71 | We use C<new_execp> to execute the first F<perl> found in the PATH. You'll |
|
|
72 | have to make sure there is one for this to work. The perl does not |
|
|
73 | actually have to be the same perl as the one running the example, and it |
|
|
74 | doesn't need to have any modules installed. |
|
|
75 | |
|
|
76 | The reason we have to specify C<perl> twice is that the first argument to |
|
|
77 | C<new_execp> (and also C<new_exec>) is the program name or path, while |
|
|
78 | the remaining ones are the arguments, and the first argument passed to a |
|
|
79 | program is the program name, so it has to be specified twice. |
|
|
80 | |
|
|
81 | Finally, the standard example, send some numbers to the remote function, |
|
|
82 | and print whatever it returns: |
|
|
83 | |
|
|
84 | my $cv = AE::cv; |
|
|
85 | |
|
|
86 | for (1..10) { |
|
|
87 | $cv->begin; |
|
|
88 | $rpc->($_, sub { |
|
|
89 | print "remote function returned: $_[0]\n"; |
|
|
90 | $cv->end; |
|
|
91 | }); |
|
|
92 | } |
|
|
93 | |
|
|
94 | $cv->recv; |
|
|
95 | |
|
|
96 | Now, executing F<perl> in the PATH isn't very interesting - you could have |
|
|
97 | done the same with L<AnyEvent::Fork>, and it might even be more efficient. |
|
|
98 | |
|
|
99 | The power of this module is that the F<perl> doesn't need to run on the |
|
|
100 | local box, you could simply substitute another command, such as F<ssh |
|
|
101 | remotebox perl>: |
|
|
102 | |
|
|
103 | my $rpc = AnyEvent::Fork::Remote |
|
|
104 | ->new_execp ("ssh", "ssh", "remotebox", "perl") |
|
|
105 | |
|
|
106 | And if you want to use a specific path for ssh, use C<new_exec>: |
|
|
107 | |
|
|
108 | my $rpc = AnyEvent::Fork::Remote |
|
|
109 | ->new_exec ("/usr/bin/ssh", "ssh", "remotebox", "perl") |
|
|
110 | |
|
|
111 | Of course, it doesn't really matter to this module how you construct your |
|
|
112 | perl processes, what matters is that somehow, you give it a file handle |
|
|
113 | connected to the new perls STDIN and STDOUT. |
49 | |
114 | |
50 | =head1 PARENT PROCESS USAGE |
115 | =head1 PARENT PROCESS USAGE |
51 | |
116 | |
52 | =over 4 |
117 | =over 4 |
53 | |
118 | |
… | |
… | |
59 | |
124 | |
60 | use Carp (); |
125 | use Carp (); |
61 | use Errno (); |
126 | use Errno (); |
62 | |
127 | |
63 | use AnyEvent (); |
128 | use AnyEvent (); |
64 | use AnyEvent::Util (); |
|
|
65 | |
129 | |
66 | our $VERSION = 0.1; |
130 | our $VERSION = '1.0'; |
67 | |
131 | |
68 | # xored together must start and and with \n |
132 | # xored together must start and and with \n |
69 | my $magic0 = "Pdk{6y[_zZ"; |
133 | my $magic0 = "Pdk{6y[_zZ"; |
70 | my $magic1 = "Z^yZ7~i=oP"; |
134 | my $magic1 = "Z^yZ7~i=oP"; |
71 | |
135 | |
… | |
… | |
77 | |
141 | |
78 | Each time a new process is needed, it executes C<$path> with the given |
142 | Each time a new process is needed, it executes C<$path> with the given |
79 | arguments (the first array member must be the program name, as with |
143 | arguments (the first array member must be the program name, as with |
80 | the C<exec> function with explicit PROGRAM argument) and both C<STDIN> |
144 | the C<exec> function with explicit PROGRAM argument) and both C<STDIN> |
81 | and C<STDOUT> connected to a communications socket. No input must be |
145 | and C<STDOUT> connected to a communications socket. No input must be |
82 | consumed by the comamnd before F<perl> is started, and no output should be |
146 | consumed by the command before F<perl> is started, and no output should be |
83 | generated. |
147 | generated. |
84 | |
148 | |
85 | The program I<must> invoke F<perl> somehow, with STDIN and STDOUT intact, |
149 | The program I<must> invoke F<perl> somehow, with STDIN and STDOUT intact, |
86 | without specifying anything to execute (no script file name, no C<-e> |
150 | without specifying anything to execute (no script file name, no C<-e> |
87 | switch etc.). |
151 | switch etc.). |
… | |
… | |
113 | |
177 | |
114 | $proc = new_execp AnyEvent::Fork::Remote "ssh", "ssh", "otherhost", "perl"; |
178 | $proc = new_execp AnyEvent::Fork::Remote "ssh", "ssh", "otherhost", "perl"; |
115 | |
179 | |
116 | =item my $proc = new AnyEvent::Fork::Remote $create_callback |
180 | =item my $proc = new AnyEvent::Fork::Remote $create_callback |
117 | |
181 | |
118 | Basically the same as C<new_exec>, but instead of a hardcoded command |
182 | Basically the same as C<new_exec>, but instead of a command to execute, |
119 | path, it expects a callback which is invoked each time a process needs to |
183 | it expects a callback which is invoked each time a process needs to be |
120 | be created. |
184 | created. |
121 | |
185 | |
122 | The C<$create_callback> is called with another callback as argument, |
186 | The C<$create_callback> is called with another callback as argument, |
123 | and should call this callback with the file handle that is connected |
187 | and should call this callback with the file handle that is connected |
124 | to a F<perl> process. This callback can be invoked even after the |
188 | to a F<perl> process. This callback can be invoked even after the |
125 | C<$create_callback> returns. |
189 | C<$create_callback> returns. |
… | |
… | |
172 | |
236 | |
173 | sub new_from_fh { |
237 | sub new_from_fh { |
174 | my ($class, @fh) = @_; |
238 | my ($class, @fh) = @_; |
175 | |
239 | |
176 | $class->new (sub { |
240 | $class->new (sub { |
177 | shift @fh |
241 | my $fh = shift @fh |
178 | or Carp::croak "AnyEvent::Fork::Remote::new_from_fh does not support fork"; |
242 | or Carp::croak "AnyEvent::Fork::Remote::new_from_fh does not support fork"; |
|
|
243 | |
|
|
244 | $_[0]($fh); |
179 | }); |
245 | }); |
180 | } |
246 | } |
181 | |
247 | |
182 | sub _new_exec { |
248 | sub _new_exec { |
183 | my $p = pop; |
249 | my $p = pop; |
… | |
… | |
286 | $linecode =~ s/\s+/ /g; # takes care of \n |
352 | $linecode =~ s/\s+/ /g; # takes care of \n |
287 | $linecode =~ s/"/''/g; |
353 | $linecode =~ s/"/''/g; |
288 | substr $linecode, 70, length $linecode, "..." if length $linecode > 70; |
354 | substr $linecode, 70, length $linecode, "..." if length $linecode > 70; |
289 | |
355 | |
290 | $self->[1] .= '{ local @_ = ' . (aq @args) . ";\n#line 1 \"'$linecode'\"\n$perlcode;\n}\n"; |
356 | $self->[1] .= '{ local @_ = ' . (aq @args) . ";\n#line 1 \"'$linecode'\"\n$perlcode;\n}\n"; |
|
|
357 | |
|
|
358 | $self |
291 | } |
359 | } |
292 | |
360 | |
293 | =item $proc = $proc->require ($module, ...) |
361 | =item $proc = $proc->require ($module, ...) |
294 | |
362 | |
295 | Quite the same as the same method of L<AnyEvent::Fork>. |
363 | Quite the same as the same method of L<AnyEvent::Fork>. |
… | |
… | |
322 | =item $proc->run ($func, $cb->($fh)) |
390 | =item $proc->run ($func, $cb->($fh)) |
323 | |
391 | |
324 | Very similar to the run method of L<AnyEvent::Fork>. |
392 | Very similar to the run method of L<AnyEvent::Fork>. |
325 | |
393 | |
326 | On the parent side, the API is identical, except that a C<$cb> argument of |
394 | On the parent side, the API is identical, except that a C<$cb> argument of |
327 | C<undef> instad of a valid file handle signals an error. |
395 | C<undef> instead of a valid file handle signals an error. |
328 | |
396 | |
329 | On the child side, the "communications socket" is in fact just C<*STDIN>, |
397 | On the child side, the "communications socket" is in fact just C<*STDIN>, |
330 | and typically can only be read from (this highly depends on how the |
398 | and typically can only be read from (this highly depends on how the |
331 | program is created - if you just run F<perl> locally, it will work for |
399 | program is created - if you just run F<perl> locally, it will work for |
332 | both reading and writing, but commands such as F<rsh> or F<ssh> typically |
400 | both reading and writing, but commands such as F<rsh> or F<ssh> typically |
… | |
… | |
356 | |
424 | |
357 | $self->[0](sub { |
425 | $self->[0](sub { |
358 | my $fh = shift |
426 | my $fh = shift |
359 | or die "AnyEvent::Fork::Remote: create callback failed"; |
427 | or die "AnyEvent::Fork::Remote: create callback failed"; |
360 | |
428 | |
361 | my $code = 'BEGIN {' . $self->[1] . "}\n" |
429 | my $owner = length $ENV{HOSTNAME} ? "$ENV{HOSTNAME}:$$" : "*:$$"; |
|
|
430 | |
|
|
431 | my $code = 'BEGIN { $0 = ' . (sq "$owner $func") . '; ' . $self->[1] . "}\n" |
362 | . 'syswrite STDOUT, ' . (sq $magic0) . '^' . (sq $magic1) . ';' |
432 | . 'syswrite STDOUT, ' . (sq $magic0) . '^' . (sq $magic1) . ';' |
363 | . '{ sysread STDIN, my $dummy, 1 }' |
433 | . '{ sysread STDIN, my $dummy, 1 }' |
364 | . "\n$func*STDIN," . (aq @{ $self->[2] }) . ';' |
434 | . "\n$func*STDIN," . (aq @{ $self->[2] }) . ';' |
365 | . "\n__END__\n"; |
435 | . "\n__END__\n"; |
366 | |
|
|
367 | warn $code;#d# |
|
|
368 | |
436 | |
369 | AnyEvent::Util::fh_nonblocking $fh, 1; |
437 | AnyEvent::Util::fh_nonblocking $fh, 1; |
370 | |
438 | |
371 | my ($rw, $ww); |
439 | my ($rw, $ww); |
372 | |
440 | |