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.6 by root, Wed Apr 17 19:43:48 2013 UTC vs.
Revision 1.8 by root, Wed Apr 17 20:24:36 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
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
253If you need an external module for serialisation, then you can either 256If you need an external module for serialisation, then you can either
254pre-load it into your L<AnyEvent::Fork> process, or you can add a C<use> 257pre-load it into your L<AnyEvent::Fork> process, or you can add a C<use>
255or C<require> statement into the serialiser string. Or both. 258or C<require> statement into the serialiser string. Or both.
256 259
257=back 260=back
261
262See the examples section earlier in this document for some actual examples.
258 263
259=cut 264=cut
260 265
261our $STRING_SERIALISER = '(sub { pack "(w/a*)*", @_ }, sub { unpack "(w/a*)*", shift })'; 266our $STRING_SERIALISER = '(sub { pack "(w/a*)*", @_ }, sub { unpack "(w/a*)*", shift })';
262 267
308 $rw = AE::io $fh, 0, sub { 313 $rw = AE::io $fh, 0, sub {
309 $rlen = $rlen * 2 + 16 if $rlen - 128 < length $rbuf; 314 $rlen = $rlen * 2 + 16 if $rlen - 128 < length $rbuf;
310 my $len = sysread $fh, $rbuf, $rlen - length $rbuf, length $rbuf; 315 my $len = sysread $fh, $rbuf, $rlen - length $rbuf, length $rbuf;
311 316
312 if ($len) { 317 if ($len) {
313 while (5 <= length $rbuf) { 318 while (4 <= length $rbuf) {
314 $len = unpack "L", $rbuf; 319 $len = unpack "L", $rbuf;
315 4 + $len <= length $rbuf 320 4 + $len <= length $rbuf
316 or last; 321 or last;
317 322
318 my @r = $t->(substr $rbuf, 4, $len); 323 my @r = $t->(substr $rbuf, 4, $len);
379 384
380The other thing that can be done with the RPC object is to destroy it. In 385The other thing that can be done with the RPC object is to destroy it. In
381this case, the child process will execute all remaining RPC calls, report 386this case, the child process will execute all remaining RPC calls, report
382their results, and then exit. 387their results, and then exit.
383 388
389See the examples section earlier in this document for some actual
390examples.
391
384=back 392=back
385 393
386=head1 CHILD PROCESS USAGE 394=head1 CHILD PROCESS USAGE
387 395
388The following function is not available in this module. They are only 396The following function is not available in this module. They are only
396 404
397Send an event to the parent. Events are a bit like RPC calls made by the 405Send an event to the parent. Events are a bit like RPC calls made by the
398child process to the parent, except that there is no notion of return 406child process to the parent, except that there is no notion of return
399values. 407values.
400 408
409See the examples section earlier in this document for some actual
410examples.
411
401=back 412=back
402 413
403=head1 SEE ALSO 414=head1 SEE ALSO
404 415
405L<AnyEvent::Fork> (to create the processes in the first place), 416L<AnyEvent::Fork> (to create the processes in the first place),

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines