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