ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent-Fork-RPC/RPC.pm
(Generate patch)

Comparing AnyEvent-Fork-RPC/RPC.pm (file contents):
Revision 1.5 by root, Wed Apr 17 19:39:54 2013 UTC vs.
Revision 1.7 by root, Wed Apr 17 20:19:41 2013 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines