1 |
=head1 NAME |
2 |
|
3 |
AnyEvent::Fork::RPC - simple RPC extension for AnyEvent::Fork |
4 |
|
5 |
=head1 SYNOPSIS |
6 |
|
7 |
use AnyEvent::Fork::RPC; |
8 |
# use AnyEvent::Fork is not needed |
9 |
|
10 |
my $rpc = AnyEvent::Fork |
11 |
->new |
12 |
->require ("MyModule") |
13 |
->AnyEvent::Fork::RPC::run ( |
14 |
"MyModule::server", |
15 |
); |
16 |
|
17 |
my $cv = AE::cv; |
18 |
|
19 |
$rpc->(1, 2, 3, sub { |
20 |
print "MyModule::server returned @_\n"; |
21 |
$cv->send; |
22 |
}); |
23 |
|
24 |
$cv->recv; |
25 |
|
26 |
=head1 DESCRIPTION |
27 |
|
28 |
This module implements a simple RPC protocol and backend for processes |
29 |
created via L<AnyEvent::Fork>, allowing you to call a function in the |
30 |
child process and receive its return values (up to 4GB serialised). |
31 |
|
32 |
It implements two different backends: a synchronous one that works like a |
33 |
normal function call, and an asynchronous one that can run multiple jobs |
34 |
concurrently in the child, using AnyEvent. |
35 |
|
36 |
It also implements an asynchronous event mechanism from the child to the |
37 |
parent, that could be used for progress indications or other information. |
38 |
|
39 |
Loading this module also always loads L<AnyEvent::Fork>, so you can make a |
40 |
separate C<use AnyEvent::Fork> if you wish, but you don't have to. |
41 |
|
42 |
=head1 EXAMPLES |
43 |
|
44 |
=head2 Example 1: Synchronous Backend |
45 |
|
46 |
Here is a simple example that implements a backend that executes C<unlink> |
47 |
and C<rmdir> calls, and reports their status back. It also reports the |
48 |
number of requests it has processed every three requests, which is clearly |
49 |
silly, but illustrates the use of events. |
50 |
|
51 |
First the parent process: |
52 |
|
53 |
use AnyEvent; |
54 |
use AnyEvent::Fork::RPC; |
55 |
|
56 |
my $done = AE::cv; |
57 |
|
58 |
my $rpc = AnyEvent::Fork |
59 |
->new |
60 |
->require ("MyWorker") |
61 |
->AnyEvent::Fork::RPC::run ("MyWorker::run", |
62 |
on_error => sub { warn "FATAL: $_[0]"; exit 1 }, |
63 |
on_event => sub { warn "$_[0] requests handled\n" }, |
64 |
on_destroy => $done, |
65 |
); |
66 |
|
67 |
for my $id (1..6) { |
68 |
$rpc->(rmdir => "/tmp/somepath/$id", sub { |
69 |
$_[0] |
70 |
or warn "/tmp/somepath/$id: $_[1]\n"; |
71 |
}); |
72 |
} |
73 |
|
74 |
undef $rpc; |
75 |
|
76 |
$done->recv; |
77 |
|
78 |
The parent creates the process, queues a few rmdir's. It then forgets |
79 |
about the C<$rpc> object, so that the child exits after it has handled the |
80 |
requests, and then it waits till the requests have been handled. |
81 |
|
82 |
The child is implemented using a separate module, C<MyWorker>, shown here: |
83 |
|
84 |
package MyWorker; |
85 |
|
86 |
my $count; |
87 |
|
88 |
sub run { |
89 |
my ($cmd, $path) = @_; |
90 |
|
91 |
AnyEvent::Fork::RPC::event ($count) |
92 |
unless ++$count % 3; |
93 |
|
94 |
my $status = $cmd eq "rmdir" ? rmdir $path |
95 |
: $cmd eq "unlink" ? unlink $path |
96 |
: die "fatal error, illegal command '$cmd'"; |
97 |
|
98 |
$status or (0, "$!") |
99 |
} |
100 |
|
101 |
1 |
102 |
|
103 |
The C<run> function first sends a "progress" event every three calls, and |
104 |
then executes C<rmdir> or C<unlink>, depending on the first parameter (or |
105 |
dies with a fatal error - obviously, you must never let this happen :). |
106 |
|
107 |
Eventually it returns the status value true if the command was successful, |
108 |
or the status value 0 and the stringified error message. |
109 |
|
110 |
On my system, running the first code fragment with the given |
111 |
F<MyWorker.pm> in the current directory yields: |
112 |
|
113 |
/tmp/somepath/1: No such file or directory |
114 |
/tmp/somepath/2: No such file or directory |
115 |
3 requests handled |
116 |
/tmp/somepath/3: No such file or directory |
117 |
/tmp/somepath/4: No such file or directory |
118 |
/tmp/somepath/5: No such file or directory |
119 |
6 requests handled |
120 |
/tmp/somepath/6: No such file or directory |
121 |
|
122 |
Obviously, none of the directories I am trying to delete even exist. Also, |
123 |
the events and responses are processed in exactly the same order as |
124 |
they were created in the child, which is true for both synchronous and |
125 |
asynchronous backends. |
126 |
|
127 |
Note that the parentheses in the call to C<AnyEvent::Fork::RPC::event> are |
128 |
not optional. That is because the function isn't defined when the code is |
129 |
compiled. You can make sure it is visible by pre-loading the correct |
130 |
backend module in the call to C<require>: |
131 |
|
132 |
->require ("AnyEvent::Fork::RPC::Sync", "MyWorker") |
133 |
|
134 |
Since the backend module declares the C<event> function, loading it first |
135 |
ensures that perl will correctly interpret calls to it. |
136 |
|
137 |
And as a final remark, there is a fine module on CPAN that can |
138 |
asynchronously C<rmdir> and C<unlink> and a lot more, and more efficiently |
139 |
than this example, namely L<IO::AIO>. |
140 |
|
141 |
=head3 Example 1a: the same with the asynchronous backend |
142 |
|
143 |
This example only shows what needs to be changed to use the async backend |
144 |
instead. Doing this is not very useful, the purpose of this example is |
145 |
to show the minimum amount of change that is required to go from the |
146 |
synchronous to the asynchronous backend. |
147 |
|
148 |
To use the async backend in the previous example, you need to add the |
149 |
C<async> parameter to the C<AnyEvent::Fork::RPC::run> call: |
150 |
|
151 |
->AnyEvent::Fork::RPC::run ("MyWorker::run", |
152 |
async => 1, |
153 |
... |
154 |
|
155 |
And since the function call protocol is now changed, you need to adopt |
156 |
C<MyWorker::run> to the async API. |
157 |
|
158 |
First, you need to accept the extra initial C<$done> callback: |
159 |
|
160 |
sub run { |
161 |
my ($done, $cmd, $path) = @_; |
162 |
|
163 |
And since a response is now generated when C<$done> is called, as opposed |
164 |
to when the function returns, we need to call the C<$done> function with |
165 |
the status: |
166 |
|
167 |
$done->($status or (0, "$!")); |
168 |
|
169 |
A few remarks are in order. First, it's quite pointless to use the async |
170 |
backend for this example - but it I<is> possible. Second, you can call |
171 |
C<$done> before or after returning from the function. Third, having both |
172 |
returned from the function and having called the C<$done> callback, the |
173 |
child process may exit at any time, so you should call C<$done> only when |
174 |
you really I<are> done. |
175 |
|
176 |
=head2 Example 2: Asynchronous Backend |
177 |
|
178 |
This example implements multiple count-downs in the child, using |
179 |
L<AnyEvent> timers. While this is a bit silly (one could use timers in te |
180 |
parent just as well), it illustrates the ability to use AnyEvent in the |
181 |
child and the fact that responses can arrive in a different order then the |
182 |
requests. |
183 |
|
184 |
It also shows how to embed the actual child code into a C<__DATA__> |
185 |
section, so it doesn't need any external files at all. |
186 |
|
187 |
And when your parent process is often busy, and you have stricter timing |
188 |
requirements, then running timers in a child process suddenly doesn't look |
189 |
so silly anymore. |
190 |
|
191 |
Without further ado, here is the code: |
192 |
|
193 |
use AnyEvent; |
194 |
use AnyEvent::Fork::RPC; |
195 |
|
196 |
my $done = AE::cv; |
197 |
|
198 |
my $rpc = AnyEvent::Fork |
199 |
->new |
200 |
->require ("AnyEvent::Fork::RPC::Async") |
201 |
->eval (do { local $/; <DATA> }) |
202 |
->AnyEvent::Fork::RPC::run ("run", |
203 |
async => 1, |
204 |
on_error => sub { warn "FATAL: $_[0]"; exit 1 }, |
205 |
on_event => sub { print $_[0] }, |
206 |
on_destroy => $done, |
207 |
); |
208 |
|
209 |
for my $count (3, 2, 1) { |
210 |
$rpc->($count, sub { |
211 |
warn "job $count finished\n"; |
212 |
}); |
213 |
} |
214 |
|
215 |
undef $rpc; |
216 |
|
217 |
$done->recv; |
218 |
|
219 |
__DATA__ |
220 |
|
221 |
# this ends up in main, as we don't use a package declaration |
222 |
|
223 |
use AnyEvent; |
224 |
|
225 |
sub run { |
226 |
my ($done, $count) = @_; |
227 |
|
228 |
my $n; |
229 |
|
230 |
AnyEvent::Fork::RPC::event "starting to count up to $count\n"; |
231 |
|
232 |
my $w; $w = AE::timer 1, 1, sub { |
233 |
++$n; |
234 |
|
235 |
AnyEvent::Fork::RPC::event "count $n of $count\n"; |
236 |
|
237 |
if ($n == $count) { |
238 |
undef $w; |
239 |
$done->(); |
240 |
} |
241 |
}; |
242 |
} |
243 |
|
244 |
The parent part (the one before the C<__DATA__> section) isn't very |
245 |
different from the earlier examples. It sets async mode, preloads |
246 |
the backend module (so the C<AnyEvent::Fork::RPC::event> function is |
247 |
declared), uses a slightly different C<on_event> handler (which we use |
248 |
simply for logging purposes) and then, instead of loading a module with |
249 |
the actual worker code, it C<eval>'s the code from the data section in the |
250 |
child process. |
251 |
|
252 |
It then starts three countdowns, from 3 to 1 seconds downwards, destroys |
253 |
the rpc object so the example finishes eventually, and then just waits for |
254 |
the stuff to trickle in. |
255 |
|
256 |
The worker code uses the event function to log some progress messages, but |
257 |
mostly just creates a recurring one-second timer. |
258 |
|
259 |
The timer callback increments a counter, logs a message, and eventually, |
260 |
when the count has been reached, calls the finish callback. |
261 |
|
262 |
On my system, this results in the following output. Since all timers fire |
263 |
at roughly the same time, the actual order isn't guaranteed, but the order |
264 |
shown is very likely what you would get, too. |
265 |
|
266 |
starting to count up to 3 |
267 |
starting to count up to 2 |
268 |
starting to count up to 1 |
269 |
count 1 of 3 |
270 |
count 1 of 2 |
271 |
count 1 of 1 |
272 |
job 1 finished |
273 |
count 2 of 2 |
274 |
job 2 finished |
275 |
count 2 of 3 |
276 |
count 3 of 3 |
277 |
job 3 finished |
278 |
|
279 |
While the overall ordering isn't guaranteed, the async backend still |
280 |
guarantees that events and responses are delivered to the parent process |
281 |
in the exact same ordering as they were generated in the child process. |
282 |
|
283 |
And unless your system is I<very> busy, it should clearly show that the |
284 |
job started last will finish first, as it has the lowest count. |
285 |
|
286 |
This concludes the async example. Since L<AnyEvent::Fork> does not |
287 |
actually fork, you are free to use about any module in the child, not just |
288 |
L<AnyEvent>, but also L<IO::AIO>, or L<Tk> for example. |
289 |
|
290 |
=head1 PARENT PROCESS USAGE |
291 |
|
292 |
This module exports nothing, and only implements a single function: |
293 |
|
294 |
=over 4 |
295 |
|
296 |
=cut |
297 |
|
298 |
package AnyEvent::Fork::RPC; |
299 |
|
300 |
use common::sense; |
301 |
|
302 |
use Errno (); |
303 |
use Guard (); |
304 |
|
305 |
use AnyEvent; |
306 |
use AnyEvent::Fork; # we don't actually depend on it, this is for convenience |
307 |
|
308 |
our $VERSION = 0.1; |
309 |
|
310 |
=item my $rpc = AnyEvent::Fork::RPC::run $fork, $function, [key => value...] |
311 |
|
312 |
The traditional way to call it. But it is way cooler to call it in the |
313 |
following way: |
314 |
|
315 |
=item my $rpc = $fork->AnyEvent::Fork::RPC::run ($function, [key => value...]) |
316 |
|
317 |
This C<run> function/method can be used in place of the |
318 |
L<AnyEvent::Fork::run> method. Just like that method, it takes over |
319 |
the L<AnyEvent::Fork> process, but instead of calling the specified |
320 |
C<$function> directly, it runs a server that accepts RPC calls and handles |
321 |
responses. |
322 |
|
323 |
It returns a function reference that can be used to call the function in |
324 |
the child process, handling serialisation and data transfers. |
325 |
|
326 |
The following key/value pairs are allowed. It is recommended to have at |
327 |
least an C<on_error> or C<on_event> handler set. |
328 |
|
329 |
=over 4 |
330 |
|
331 |
=item on_error => $cb->($msg) |
332 |
|
333 |
Called on (fatal) errors, with a descriptive (hopefully) message. If |
334 |
this callback is not provided, but C<on_event> is, then the C<on_event> |
335 |
callback is called with the first argument being the string C<error>, |
336 |
followed by the error message. |
337 |
|
338 |
If neither handler is provided it prints the error to STDERR and will |
339 |
start failing badly. |
340 |
|
341 |
=item on_event => $cb->(...) |
342 |
|
343 |
Called for every call to the C<AnyEvent::Fork::RPC::event> function in the |
344 |
child, with the arguments of that function passed to the callback. |
345 |
|
346 |
Also called on errors when no C<on_error> handler is provided. |
347 |
|
348 |
=item on_destroy => $cb->() |
349 |
|
350 |
Called when the C<$rpc> object has been destroyed and all requests have |
351 |
been successfully handled. This is useful when you queue some requests and |
352 |
want the child to go away after it has handled them. The problem is that |
353 |
the parent must not exit either until all requests have been handled, and |
354 |
this can be accomplished by waiting for this callback. |
355 |
|
356 |
=item init => $function (default none) |
357 |
|
358 |
When specified (by name), this function is called in the child as the very |
359 |
first thing when taking over the process, with all the arguments normally |
360 |
passed to the C<AnyEvent::Fork::run> function, except the communications |
361 |
socket. |
362 |
|
363 |
It can be used to do one-time things in the child such as storing passed |
364 |
parameters or opening database connections. |
365 |
|
366 |
It is called very early - before the serialisers are created or the |
367 |
C<$function> name is resolved into a function reference, so it could be |
368 |
used to load any modules that provide the serialiser or function. It can |
369 |
not, however, create events. |
370 |
|
371 |
=item async => $boolean (default: 0) |
372 |
|
373 |
The default server used in the child does all I/O blockingly, and only |
374 |
allows a single RPC call to execute concurrently. |
375 |
|
376 |
Setting C<async> to a true value switches to another implementation that |
377 |
uses L<AnyEvent> in the child and allows multiple concurrent RPC calls (it |
378 |
does not support recursion in the event loop however, blocking condvar |
379 |
calls will fail). |
380 |
|
381 |
The actual API in the child is documented in the section that describes |
382 |
the calling semantics of the returned C<$rpc> function. |
383 |
|
384 |
If you want to pre-load the actual back-end modules to enable memory |
385 |
sharing, then you should load C<AnyEvent::Fork::RPC::Sync> for |
386 |
synchronous, and C<AnyEvent::Fork::RPC::Async> for asynchronous mode. |
387 |
|
388 |
If you use a template process and want to fork both sync and async |
389 |
children, then it is permissible to load both modules. |
390 |
|
391 |
=item serialiser => $string (default: $AnyEvent::Fork::RPC::STRING_SERIALISER) |
392 |
|
393 |
All arguments, result data and event data have to be serialised to be |
394 |
transferred between the processes. For this, they have to be frozen and |
395 |
thawed in both parent and child processes. |
396 |
|
397 |
By default, only octet strings can be passed between the processes, which |
398 |
is reasonably fast and efficient and requires no extra modules. |
399 |
|
400 |
For more complicated use cases, you can provide your own freeze and thaw |
401 |
functions, by specifying a string with perl source code. It's supposed to |
402 |
return two code references when evaluated: the first receives a list of |
403 |
perl values and must return an octet string. The second receives the octet |
404 |
string and must return the original list of values. |
405 |
|
406 |
If you need an external module for serialisation, then you can either |
407 |
pre-load it into your L<AnyEvent::Fork> process, or you can add a C<use> |
408 |
or C<require> statement into the serialiser string. Or both. |
409 |
|
410 |
Here are some examples - some of them are also available as global |
411 |
variables that make them easier to use. |
412 |
|
413 |
=over 4 |
414 |
|
415 |
=item octet strings - C<$AnyEvent::Fork::RPC::STRING_SERIALISER> |
416 |
|
417 |
This serialiser concatenates length-prefixes octet strings, and is the |
418 |
default. |
419 |
|
420 |
Implementation: |
421 |
|
422 |
( |
423 |
sub { pack "(w/a*)*", @_ }, |
424 |
sub { unpack "(w/a*)*", shift } |
425 |
) |
426 |
|
427 |
=item json - C<$AnyEvent::Fork::RPC::JSON_SERIALISER> |
428 |
|
429 |
This serialiser creates JSON arrays - you have to make sure the L<JSON> |
430 |
module is installed for this serialiser to work. It can be beneficial for |
431 |
sharing when you preload the L<JSON> module in a template process. |
432 |
|
433 |
L<JSON> (with L<JSON::XS> installed) is slower than the octet string |
434 |
serialiser, but usually much faster than L<Storable>, unless big chunks of |
435 |
binary data need to be transferred. |
436 |
|
437 |
Implementation: |
438 |
|
439 |
use JSON (); |
440 |
( |
441 |
sub { JSON::encode_json \@_ }, |
442 |
sub { @{ JSON::decode_json shift } } |
443 |
) |
444 |
|
445 |
=item storable - C<$AnyEvent::Fork::RPC::STORABLE_SERIALISER> |
446 |
|
447 |
This serialiser uses L<Storable>, which means it has high chance of |
448 |
serialising just about anything you throw at it, at the cost of having |
449 |
very high overhead per operation. It also comes with perl. |
450 |
|
451 |
Implementation: |
452 |
|
453 |
use Storable (); |
454 |
( |
455 |
sub { Storable::freeze \@_ }, |
456 |
sub { @{ Storable::thaw shift } } |
457 |
) |
458 |
|
459 |
=back |
460 |
|
461 |
=back |
462 |
|
463 |
See the examples section earlier in this document for some actual |
464 |
examples. |
465 |
|
466 |
=cut |
467 |
|
468 |
our $STRING_SERIALISER = '(sub { pack "(w/a*)*", @_ }, sub { unpack "(w/a*)*", shift })'; |
469 |
our $JSON_SERIALISER = 'use JSON (); (sub { JSON::encode_json \@_ }, sub { @{ JSON::decode_json shift } })'; |
470 |
our $STORABLE_SERIALISER = 'use Storable (); (sub { Storable::freeze \@_ }, sub { @{ Storable::thaw shift } })'; |
471 |
|
472 |
sub run { |
473 |
my ($self, $function, %arg) = @_; |
474 |
|
475 |
my $serialiser = delete $arg{serialiser} || $STRING_SERIALISER; |
476 |
my $on_event = delete $arg{on_event}; |
477 |
my $on_error = delete $arg{on_error}; |
478 |
my $on_destroy = delete $arg{on_destroy}; |
479 |
|
480 |
# default for on_error is to on_event, if specified |
481 |
$on_error ||= $on_event |
482 |
? sub { $on_event->(error => shift) } |
483 |
: sub { die "AnyEvent::Fork::RPC: uncaught error: $_[0].\n" }; |
484 |
|
485 |
# default for on_event is to raise an error |
486 |
$on_event ||= sub { $on_error->("event received, but no on_event handler") }; |
487 |
|
488 |
my ($f, $t) = eval $serialiser; die $@ if $@; |
489 |
|
490 |
my (@rcb, %rcb, $fh, $shutdown, $wbuf, $ww); |
491 |
my ($rlen, $rbuf, $rw) = 512 - 16; |
492 |
|
493 |
my $wcb = sub { |
494 |
my $len = syswrite $fh, $wbuf; |
495 |
|
496 |
unless (defined $len) { |
497 |
if ($! != Errno::EAGAIN && $! != Errno::EWOULDBLOCK) { |
498 |
undef $rw; undef $ww; # it ends here |
499 |
$on_error->("$!"); |
500 |
} |
501 |
} |
502 |
|
503 |
substr $wbuf, 0, $len, ""; |
504 |
|
505 |
unless (length $wbuf) { |
506 |
undef $ww; |
507 |
$shutdown and shutdown $fh, 1; |
508 |
} |
509 |
}; |
510 |
|
511 |
my $module = "AnyEvent::Fork::RPC::" . ($arg{async} ? "Async" : "Sync"); |
512 |
|
513 |
$self->require ($module) |
514 |
->send_arg ($function, $arg{init}, $serialiser) |
515 |
->run ("$module\::run", sub { |
516 |
$fh = shift; |
517 |
|
518 |
my ($id, $len); |
519 |
$rw = AE::io $fh, 0, sub { |
520 |
$rlen = $rlen * 2 + 16 if $rlen - 128 < length $rbuf; |
521 |
$len = sysread $fh, $rbuf, $rlen - length $rbuf, length $rbuf; |
522 |
|
523 |
if ($len) { |
524 |
while (8 <= length $rbuf) { |
525 |
($id, $len) = unpack "LL", $rbuf; |
526 |
8 + $len <= length $rbuf |
527 |
or last; |
528 |
|
529 |
my @r = $t->(substr $rbuf, 8, $len); |
530 |
substr $rbuf, 0, 8 + $len, ""; |
531 |
|
532 |
if ($id) { |
533 |
if (@rcb) { |
534 |
(shift @rcb)->(@r); |
535 |
} elsif (my $cb = delete $rcb{$id}) { |
536 |
$cb->(@r); |
537 |
} else { |
538 |
undef $rw; undef $ww; |
539 |
$on_error->("unexpected data from child"); |
540 |
} |
541 |
} else { |
542 |
$on_event->(@r); |
543 |
} |
544 |
} |
545 |
} elsif (defined $len) { |
546 |
undef $rw; undef $ww; # it ends here |
547 |
|
548 |
if (@rcb || %rcb) { |
549 |
$on_error->("unexpected eof"); |
550 |
} else { |
551 |
$on_destroy->(); |
552 |
} |
553 |
} elsif ($! != Errno::EAGAIN && $! != Errno::EWOULDBLOCK) { |
554 |
undef $rw; undef $ww; # it ends here |
555 |
$on_error->("read: $!"); |
556 |
} |
557 |
}; |
558 |
|
559 |
$ww ||= AE::io $fh, 1, $wcb; |
560 |
}); |
561 |
|
562 |
my $guard = Guard::guard { |
563 |
$shutdown = 1; |
564 |
$ww ||= $fh && AE::io $fh, 1, $wcb; |
565 |
}; |
566 |
|
567 |
my $id; |
568 |
|
569 |
$arg{async} |
570 |
? sub { |
571 |
$id = ($id == 0xffffffff ? 0 : $id) + 1; |
572 |
$id = ($id == 0xffffffff ? 0 : $id) + 1 while exists $rcb{$id}; # rarely loops |
573 |
|
574 |
$rcb{$id} = pop; |
575 |
|
576 |
$guard; # keep it alive |
577 |
|
578 |
$wbuf .= pack "LL/a*", $id, &$f; |
579 |
$ww ||= $fh && AE::io $fh, 1, $wcb; |
580 |
} |
581 |
: sub { |
582 |
push @rcb, pop; |
583 |
|
584 |
$guard; # keep it alive |
585 |
|
586 |
$wbuf .= pack "L/a*", &$f; |
587 |
$ww ||= $fh && AE::io $fh, 1, $wcb; |
588 |
} |
589 |
} |
590 |
|
591 |
=item $rpc->(..., $cb->(...)) |
592 |
|
593 |
The RPC object returned by C<AnyEvent::Fork::RPC::run> is actually a code |
594 |
reference. There are two things you can do with it: call it, and let it go |
595 |
out of scope (let it get destroyed). |
596 |
|
597 |
If C<async> was false when C<$rpc> was created (the default), then, if you |
598 |
call C<$rpc>, the C<$function> is invoked with all arguments passed to |
599 |
C<$rpc> except the last one (the callback). When the function returns, the |
600 |
callback will be invoked with all the return values. |
601 |
|
602 |
If C<async> was true, then the C<$function> receives an additional |
603 |
initial argument, the result callback. In this case, returning from |
604 |
C<$function> does nothing - the function only counts as "done" when the |
605 |
result callback is called, and any arguments passed to it are considered |
606 |
the return values. This makes it possible to "return" from event handlers |
607 |
or e.g. Coro threads. |
608 |
|
609 |
The other thing that can be done with the RPC object is to destroy it. In |
610 |
this case, the child process will execute all remaining RPC calls, report |
611 |
their results, and then exit. |
612 |
|
613 |
See the examples section earlier in this document for some actual |
614 |
examples. |
615 |
|
616 |
=back |
617 |
|
618 |
=head1 CHILD PROCESS USAGE |
619 |
|
620 |
The following function is not available in this module. They are only |
621 |
available in the namespace of this module when the child is running, |
622 |
without having to load any extra modules. They are part of the child-side |
623 |
API of L<AnyEvent::Fork::RPC>. |
624 |
|
625 |
=over 4 |
626 |
|
627 |
=item AnyEvent::Fork::RPC::event ... |
628 |
|
629 |
Send an event to the parent. Events are a bit like RPC calls made by the |
630 |
child process to the parent, except that there is no notion of return |
631 |
values. |
632 |
|
633 |
See the examples section earlier in this document for some actual |
634 |
examples. |
635 |
|
636 |
=back |
637 |
|
638 |
=head1 ADVANCED TOPICS |
639 |
|
640 |
=head2 Choosing a backend |
641 |
|
642 |
So how do you decide which backend to use? Well, that's your problem to |
643 |
solve, but here are some thoughts on the matter: |
644 |
|
645 |
=over 4 |
646 |
|
647 |
=item Synchronous |
648 |
|
649 |
The synchronous backend does not rely on any external modules (well, |
650 |
except L<common::sense>, which works around a bug in how perl's warning |
651 |
system works). This keeps the process very small, for example, on my |
652 |
system, an empty perl interpreter uses 1492kB RSS, which becomes 2020kB |
653 |
after C<use warnings; use strict> (for people who grew up with C64s around |
654 |
them this is probably shocking every single time they see it). The worker |
655 |
process in the first example in this document uses 1792kB. |
656 |
|
657 |
Since the calls are done synchronously, slow jobs will keep newer jobs |
658 |
from executing. |
659 |
|
660 |
The synchronous backend also has no overhead due to running an event loop |
661 |
- reading requests is therefore very efficient, while writing responses is |
662 |
less so, as every response results in a write syscall. |
663 |
|
664 |
If the parent process is busy and a bit slow reading responses, the child |
665 |
waits instead of processing further requests. This also limits the amount |
666 |
of memory needed for buffering, as never more than one response has to be |
667 |
buffered. |
668 |
|
669 |
The API in the child is simple - you just have to define a function that |
670 |
does something and returns something. |
671 |
|
672 |
It's hard to use modules or code that relies on an event loop, as the |
673 |
child cannot execute anything while it waits for more input. |
674 |
|
675 |
=item Asynchronous |
676 |
|
677 |
The asynchronous backend relies on L<AnyEvent>, which tries to be small, |
678 |
but still comes at a price: On my system, the worker from example 1a uses |
679 |
3420kB RSS (for L<AnyEvent>, which loads L<EV>, which needs L<XSLoader> |
680 |
which in turn loads a lot of other modules such as L<warnings>, L<strict>, |
681 |
L<vars>, L<Exporter>...). |
682 |
|
683 |
It batches requests and responses reasonably efficiently, doing only as |
684 |
few reads and writes as needed, but needs to poll for events via the event |
685 |
loop. |
686 |
|
687 |
Responses are queued when the parent process is busy. This means the child |
688 |
can continue to execute any queued requests. It also means that a child |
689 |
might queue a lot of responses in memory when it generates them and the |
690 |
parent process is slow accepting them. |
691 |
|
692 |
The API is not a straightforward RPC pattern - you have to call a |
693 |
"done" callback to pass return values and signal completion. Also, more |
694 |
importantly, the API starts jobs as fast as possible - when 1000 jobs |
695 |
are queued and the jobs are slow, they will all run concurrently. The |
696 |
child must implement some queueing/limiting mechanism if this causes |
697 |
problems. Alternatively, the parent could limit the amount of rpc calls |
698 |
that are outstanding. |
699 |
|
700 |
Blocking use of condvars is not supported. |
701 |
|
702 |
Using event-based modules such as L<IO::AIO>, L<Gtk2>, L<Tk> and so on is |
703 |
easy. |
704 |
|
705 |
=back |
706 |
|
707 |
=head2 Passing file descriptors |
708 |
|
709 |
Unlike L<AnyEvent::Fork>, this module has no in-built file handle or file |
710 |
descriptor passing abilities. |
711 |
|
712 |
The reason is that passing file descriptors is extraordinary tricky |
713 |
business, and conflicts with efficient batching of messages. |
714 |
|
715 |
There still is a method you can use: Create a |
716 |
C<AnyEvent::Util::portable_socketpair> and C<send_fh> one half of it to |
717 |
the process before you pass control to C<AnyEvent::Fork::RPC::run>. |
718 |
|
719 |
Whenever you want to pass a file descriptor, send an rpc request to the |
720 |
child process (so it expects the descriptor), then send it over the other |
721 |
half of the socketpair. The child should fetch the descriptor from the |
722 |
half it has passed earlier. |
723 |
|
724 |
Here is some (untested) pseudocode to that effect: |
725 |
|
726 |
use AnyEvent::Util; |
727 |
use AnyEvent::Fork::RPC; |
728 |
use IO::FDPass; |
729 |
|
730 |
my ($s1, $s2) = AnyEvent::Util::portable_socketpair; |
731 |
|
732 |
my $rpc = AnyEvent::Fork |
733 |
->new |
734 |
->send_fh ($s2) |
735 |
->require ("MyWorker") |
736 |
->AnyEvent::Fork::RPC::run ("MyWorker::run" |
737 |
init => "MyWorker::init", |
738 |
); |
739 |
|
740 |
undef $s2; # no need to keep it around |
741 |
|
742 |
# pass an fd |
743 |
$rpc->("i'll send some fd now, please expect it!", my $cv = AE::cv); |
744 |
|
745 |
IO::FDPass fileno $s1, fileno $handle_to_pass; |
746 |
|
747 |
$cv->recv; |
748 |
|
749 |
The MyWorker module could look like this: |
750 |
|
751 |
package MyWorker; |
752 |
|
753 |
use IO::FDPass; |
754 |
|
755 |
my $s2; |
756 |
|
757 |
sub init { |
758 |
$s2 = $_[0]; |
759 |
} |
760 |
|
761 |
sub run { |
762 |
if ($_[0] eq "i'll send some fd now, please expect it!") { |
763 |
my $fd = IO::FDPass::recv fileno $s2; |
764 |
... |
765 |
} |
766 |
} |
767 |
|
768 |
Of course, this might be blocking if you pass a lot of file descriptors, |
769 |
so you might want to look into L<AnyEvent::FDpasser> which can handle the |
770 |
gory details. |
771 |
|
772 |
=head1 SEE ALSO |
773 |
|
774 |
L<AnyEvent::Fork> (to create the processes in the first place), |
775 |
L<AnyEvent::Fork::Pool> (to manage whole pools of processes). |
776 |
|
777 |
=head1 AUTHOR AND CONTACT INFORMATION |
778 |
|
779 |
Marc Lehmann <schmorp@schmorp.de> |
780 |
http://software.schmorp.de/pkg/AnyEvent-Fork-RPC |
781 |
|
782 |
=cut |
783 |
|
784 |
1 |
785 |
|