… | |
… | |
2 | |
2 | |
3 | AnyEvent::Fork::RPC - simple RPC extension for AnyEvent::Fork |
3 | AnyEvent::Fork::RPC - simple RPC extension for AnyEvent::Fork |
4 | |
4 | |
5 | =head1 SYNOPSIS |
5 | =head1 SYNOPSIS |
6 | |
6 | |
7 | use AnyEvent::Fork; |
|
|
8 | use AnyEvent::Fork::RPC; |
7 | use AnyEvent::Fork::RPC; |
|
|
8 | # use AnyEvent::Fork is not needed |
9 | |
9 | |
10 | my $rpc = AnyEvent::Fork |
10 | my $rpc = AnyEvent::Fork |
11 | ->new |
11 | ->new |
12 | ->require ("MyModule") |
12 | ->require ("MyModule") |
13 | ->AnyEvent::Fork::RPC::run ( |
13 | ->AnyEvent::Fork::RPC::run ( |
… | |
… | |
34 | concurrently in the child, using AnyEvent. |
34 | concurrently in the child, using AnyEvent. |
35 | |
35 | |
36 | It also implements an asynchronous event mechanism from the child to the |
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. |
37 | parent, that could be used for progress indications or other information. |
38 | |
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 | |
39 | =head1 EXAMPLES |
42 | =head1 EXAMPLES |
40 | |
43 | |
41 | =head2 Synchronous Backend |
44 | =head2 Synchronous Backend |
42 | |
45 | |
43 | Here is a simple example that implements a backend that executes C<unlink> |
46 | Here is a simple example that implements a backend that executes C<unlink> |
… | |
… | |
55 | |
58 | |
56 | my $rpc = AnyEvent::Fork |
59 | my $rpc = AnyEvent::Fork |
57 | ->new |
60 | ->new |
58 | ->require ("MyWorker") |
61 | ->require ("MyWorker") |
59 | ->AnyEvent::Fork::RPC::run ("MyWorker::run", |
62 | ->AnyEvent::Fork::RPC::run ("MyWorker::run", |
|
|
63 | on_error => sub { warn "FATAL: $_[0]"; exit 1 }, |
60 | on_event => sub { warn "$_[0] requests handled\n" }, |
64 | on_event => sub { warn "$_[0] requests handled\n" }, |
61 | on_destroy => $done, |
65 | on_destroy => $done, |
62 | ); |
66 | ); |
63 | |
67 | |
64 | for my $id (1..6) { |
68 | for my $id (1..6) { |
… | |
… | |
102 | dies with a fatal error - obviously, you must never let this happen :). |
106 | dies with a fatal error - obviously, you must never let this happen :). |
103 | |
107 | |
104 | Eventually it returns the status value true if the command was successful, |
108 | Eventually it returns the status value true if the command was successful, |
105 | or the status value 0 and the stringified error message. |
109 | or the status value 0 and the stringified error message. |
106 | |
110 | |
107 | On my system, running the first cdoe fragment with the given |
111 | On my system, running the first code fragment with the given |
108 | F<MyWorker.pm> in the current directory yields: |
112 | F<MyWorker.pm> in the current directory yields: |
109 | |
113 | |
110 | /tmp/somepath/1: No such file or directory |
114 | /tmp/somepath/1: No such file or directory |
111 | /tmp/somepath/2: No such file or directory |
115 | /tmp/somepath/2: No such file or directory |
112 | 3 requests handled |
116 | 3 requests handled |
… | |
… | |
149 | |
153 | |
150 | use Errno (); |
154 | use Errno (); |
151 | use Guard (); |
155 | use Guard (); |
152 | |
156 | |
153 | use AnyEvent; |
157 | use AnyEvent; |
154 | #use AnyEvent::Fork; |
158 | use AnyEvent::Fork; # we don't actually depend on it, this is for convenience |
155 | |
159 | |
156 | our $VERSION = 0.1; |
160 | our $VERSION = 0.1; |
157 | |
161 | |
158 | =item my $rpc = AnyEvent::Fork::RPC::run $fork, $function, [key => value...] |
162 | =item my $rpc = AnyEvent::Fork::RPC::run $fork, $function, [key => value...] |
159 | |
163 | |
… | |
… | |
197 | |
201 | |
198 | Called when the C<$rpc> object has been destroyed and all requests have |
202 | Called when the C<$rpc> object has been destroyed and all requests have |
199 | been successfully handled. This is useful when you queue some requests and |
203 | been successfully handled. This is useful when you queue some requests and |
200 | want the child to go away after it has handled them. The problem is that |
204 | want the child to go away after it has handled them. The problem is that |
201 | the parent must not exit either until all requests have been handled, and |
205 | the parent must not exit either until all requests have been handled, and |
202 | this cna be accomplished by waiting for this callback. |
206 | this can be accomplished by waiting for this callback. |
203 | |
207 | |
204 | =item init => $function (default none) |
208 | =item init => $function (default none) |
205 | |
209 | |
206 | When specified (by name), this function is called in the child as the very |
210 | When specified (by name), this function is called in the child as the very |
207 | first thing when taking over the process, with all the arguments normally |
211 | first thing when taking over the process, with all the arguments normally |
… | |
… | |
230 | If you want to pre-load the actual back-end modules to enable memory |
234 | If you want to pre-load the actual back-end modules to enable memory |
231 | sharing, then you should load C<AnyEvent::Fork::RPC::Sync> for |
235 | sharing, then you should load C<AnyEvent::Fork::RPC::Sync> for |
232 | synchronous, and C<AnyEvent::Fork::RPC::Async> for asynchronous mode. |
236 | synchronous, and C<AnyEvent::Fork::RPC::Async> for asynchronous mode. |
233 | |
237 | |
234 | If you use a template process and want to fork both sync and async |
238 | If you use a template process and want to fork both sync and async |
235 | children, then it is permissible to laod both modules. |
239 | children, then it is permissible to load both modules. |
236 | |
240 | |
237 | =item serialiser => $string (default: '(sub { pack "(w/a*)*", @_ }, sub { unpack "(w/a*)*", shift })') |
241 | =item serialiser => $string (default: '(sub { pack "(w/a*)*", @_ }, sub { unpack "(w/a*)*", shift })') |
238 | |
242 | |
239 | All arguments, result data and event data have to be serialised to be |
243 | All arguments, result data and event data have to be serialised to be |
240 | transferred between the processes. For this, they have to be frozen and |
244 | transferred between the processes. For this, they have to be frozen and |
… | |
… | |
307 | $rw = AE::io $fh, 0, sub { |
311 | $rw = AE::io $fh, 0, sub { |
308 | $rlen = $rlen * 2 + 16 if $rlen - 128 < length $rbuf; |
312 | $rlen = $rlen * 2 + 16 if $rlen - 128 < length $rbuf; |
309 | my $len = sysread $fh, $rbuf, $rlen - length $rbuf, length $rbuf; |
313 | my $len = sysread $fh, $rbuf, $rlen - length $rbuf, length $rbuf; |
310 | |
314 | |
311 | if ($len) { |
315 | if ($len) { |
312 | while (5 <= length $rbuf) { |
316 | while (4 <= length $rbuf) { |
313 | $len = unpack "L", $rbuf; |
317 | $len = unpack "L", $rbuf; |
314 | 4 + $len <= length $rbuf |
318 | 4 + $len <= length $rbuf |
315 | or last; |
319 | or last; |
316 | |
320 | |
317 | my @r = $t->(substr $rbuf, 4, $len); |
321 | my @r = $t->(substr $rbuf, 4, $len); |