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.1 by root, Wed Apr 17 15:55:59 2013 UTC vs.
Revision 1.3 by root, Wed Apr 17 17:16:48 2013 UTC

113uses L<AnyEvent> in the child and allows multiple concurrent RPC calls. 113uses L<AnyEvent> in the child and allows multiple concurrent RPC calls.
114 114
115The actual API in the child is documented in the section that describes 115The actual API in the child is documented in the section that describes
116the calling semantics of the returned C<$rpc> function. 116the calling semantics of the returned C<$rpc> function.
117 117
118If you want to pre-load the actual back-end modules to enable memory
119sharing, then you should load C<AnyEvent::Fork::RPC::Sync> for
120synchronous, and C<AnyEvent::Fork::RPC::Async> for asynchronous mode.
121
118=item serialiser => $string (default: '(sub { pack "(w/a*)*", @_ }, sub { unpack "(w/a*)*", shift })') 122=item serialiser => $string (default: '(sub { pack "(w/a*)*", @_ }, sub { unpack "(w/a*)*", shift })')
119 123
120All arguments, result data and event data have to be serialised to be 124All arguments, result data and event data have to be serialised to be
121transferred between the processes. For this, they have to be frozen and 125transferred between the processes. For this, they have to be frozen and
122thawed in both parent and child processes. 126thawed in both parent and child processes.
128functions, by specifying a string with perl source code. It's supposed to 132functions, by specifying a string with perl source code. It's supposed to
129return two code references when evaluated: the first receives a list of 133return two code references when evaluated: the first receives a list of
130perl values and must return an octet string. The second receives the octet 134perl values and must return an octet string. The second receives the octet
131string and must return the original list of values. 135string and must return the original list of values.
132 136
137If you need an external module for serialisation, then you can either
138pre-load it into your L<AnyEvent::Fork> process, or you can add a C<use>
139or C<require> statement into the serialiser string. Or both.
140
133=back 141=back
134 142
135=cut 143=cut
136 144
137our $SERIALISE_STRINGS = '(sub { pack "(w/a*)*", @_ }, sub { unpack "(w/a*)*", shift })'; 145our $STRING_SERIALISER = '(sub { pack "(w/a*)*", @_ }, sub { unpack "(w/a*)*", shift })';
146
147# ideally, we want (SvLEN - SvCUR) || 1024 or somesuch...
148sub rlen($) { ($_[0] < 384 ? 512 + 16 : 2 << int +(log $_[0] + 512) / log 2) - $_[0] - 16 }
138 149
139sub run { 150sub run {
140 my ($self, $function, %arg) = @_; 151 my ($self, $function, %arg) = @_;
141 152
142 my $serialiser = delete $arg{serialiser} || $SERIALISE_STRINGS; 153 my $serialiser = delete $arg{serialiser} || $STRING_SERIALISER;
143 my $on_event = delete $arg{on_event}; 154 my $on_event = delete $arg{on_event};
144 my $on_error = delete $arg{on_error}; 155 my $on_error = delete $arg{on_error};
145 156
146 # default for on_error is to on_event, if specified 157 # default for on_error is to on_event, if specified
147 $on_error ||= $on_event 158 $on_error ||= $on_event
178 $self->require ($module) 189 $self->require ($module)
179 ->send_arg ($function, $arg{init}, $serialiser) 190 ->send_arg ($function, $arg{init}, $serialiser)
180 ->run ("$module\::run", sub { 191 ->run ("$module\::run", sub {
181 $fh = shift; 192 $fh = shift;
182 $rw = AE::io $fh, 0, sub { 193 $rw = AE::io $fh, 0, sub {
183 my $len = sysread $fh, $rbuf, 512 + length $rbuf, length $rbuf; 194 my $len = sysread $fh, $rbuf, rlen length $rbuf, length $rbuf;
184 195
185 if ($len) { 196 if ($len) {
186 while (5 <= length $rbuf) { 197 while (5 <= length $rbuf) {
187 $len = unpack "L", $rbuf; 198 $len = unpack "L", $rbuf;
188 if (4 + $len <= length $rbuf) { 199 4 + $len <= length $rbuf
200 or last;
201
189 my @r = $t->(substr $rbuf, 4, $len); 202 my @r = $t->(substr $rbuf, 4, $len);
190 substr $rbuf, 0, $len + 4, ""; 203 substr $rbuf, 0, $len + 4, "";
191 204
192 if (pop @r) { 205 if (pop @r) {
193 $on_event->(@r); 206 $on_event->(@r);
194 } elsif (@rcb) { 207 } elsif (@rcb) {
195 (shift @rcb)->(@r); 208 (shift @rcb)->(@r);
196 } else { 209 } else {
197 undef $rw; undef $ww; 210 undef $rw; undef $ww;
198 $on_error->("unexpected data from child"); 211 $on_error->("unexpected data from child");
199 }
200 } 212 }
201 } 213 }
202 } elsif (defined $len) { 214 } elsif (defined $len) {
203 undef $rw; undef $ww; # it ends here 215 undef $rw; undef $ww; # it ends here
204 $on_error->("unexpected eof") 216 $on_error->("unexpected eof")
236having to load any extra module. They are part of the child-side API of 248having to load any extra module. They are part of the child-side API of
237L<AnyEvent::Fork::RPC>. 249L<AnyEvent::Fork::RPC>.
238 250
239=over 4 251=over 4
240 252
241=item AnyEvent::Fork::RPC::quit
242
243This function can be called to gracefully stop the child process when it
244is idle.
245
246After this function is called, the process stops handling incoming RPC
247requests, but outstanding events and function return values will be sent
248to the parent. When all data has been sent, the process calls C<exit>.
249
250Since the parent might not expect the child to exit at random points in
251time, it is often better to signal the parent by sending an C<event> and
252letting the parent close down the child process.
253
254=item AnyEvent::Fork::RPC::event ... 253=item AnyEvent::Fork::RPC::event ...
255 254
256Send an event to the parent. Events are a bit like RPC calls made by the 255Send an event to the parent. Events are a bit like RPC calls made by the
257child process to the parent, except that there is no notion of return 256child process to the parent, except that there is no notion of return
258values. 257values.

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines