… | |
… | |
113 | uses L<AnyEvent> in the child and allows multiple concurrent RPC calls. |
113 | uses L<AnyEvent> in the child and allows multiple concurrent RPC calls. |
114 | |
114 | |
115 | The actual API in the child is documented in the section that describes |
115 | The actual API in the child is documented in the section that describes |
116 | the calling semantics of the returned C<$rpc> function. |
116 | the calling semantics of the returned C<$rpc> function. |
117 | |
117 | |
|
|
118 | If you want to pre-load the actual back-end modules to enable memory |
|
|
119 | sharing, then you should load C<AnyEvent::Fork::RPC::Sync> for |
|
|
120 | synchronous, 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 | |
120 | All arguments, result data and event data have to be serialised to be |
124 | All arguments, result data and event data have to be serialised to be |
121 | transferred between the processes. For this, they have to be frozen and |
125 | transferred between the processes. For this, they have to be frozen and |
122 | thawed in both parent and child processes. |
126 | thawed in both parent and child processes. |
… | |
… | |
128 | functions, by specifying a string with perl source code. It's supposed to |
132 | functions, by specifying a string with perl source code. It's supposed to |
129 | return two code references when evaluated: the first receives a list of |
133 | return two code references when evaluated: the first receives a list of |
130 | perl values and must return an octet string. The second receives the octet |
134 | perl values and must return an octet string. The second receives the octet |
131 | string and must return the original list of values. |
135 | string and must return the original list of values. |
132 | |
136 | |
|
|
137 | If you need an external module for serialisation, then you can either |
|
|
138 | pre-load it into your L<AnyEvent::Fork> process, or you can add a C<use> |
|
|
139 | or C<require> statement into the serialiser string. Or both. |
|
|
140 | |
133 | =back |
141 | =back |
134 | |
142 | |
135 | =cut |
143 | =cut |
136 | |
144 | |
137 | our $SERIALISE_STRINGS = '(sub { pack "(w/a*)*", @_ }, sub { unpack "(w/a*)*", shift })'; |
145 | our $STRING_SERIALISER = '(sub { pack "(w/a*)*", @_ }, sub { unpack "(w/a*)*", shift })'; |
|
|
146 | |
|
|
147 | # ideally, we want (SvLEN - SvCUR) || 1024 or somesuch... |
|
|
148 | sub rlen($) { ($_[0] < 384 ? 512 + 16 : 2 << int +(log $_[0] + 512) / log 2) - $_[0] - 16 } |
138 | |
149 | |
139 | sub run { |
150 | sub 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") |
… | |
… | |
236 | having to load any extra module. They are part of the child-side API of |
248 | having to load any extra module. They are part of the child-side API of |
237 | L<AnyEvent::Fork::RPC>. |
249 | L<AnyEvent::Fork::RPC>. |
238 | |
250 | |
239 | =over 4 |
251 | =over 4 |
240 | |
252 | |
241 | =item AnyEvent::Fork::RPC::quit |
|
|
242 | |
|
|
243 | This function can be called to gracefully stop the child process when it |
|
|
244 | is idle. |
|
|
245 | |
|
|
246 | After this function is called, the process stops handling incoming RPC |
|
|
247 | requests, but outstanding events and function return values will be sent |
|
|
248 | to the parent. When all data has been sent, the process calls C<exit>. |
|
|
249 | |
|
|
250 | Since the parent might not expect the child to exit at random points in |
|
|
251 | time, it is often better to signal the parent by sending an C<event> and |
|
|
252 | letting the parent close down the child process. |
|
|
253 | |
|
|
254 | =item AnyEvent::Fork::RPC::event ... |
253 | =item AnyEvent::Fork::RPC::event ... |
255 | |
254 | |
256 | Send an event to the parent. Events are a bit like RPC calls made by the |
255 | Send an event to the parent. Events are a bit like RPC calls made by the |
257 | child process to the parent, except that there is no notion of return |
256 | child process to the parent, except that there is no notion of return |
258 | values. |
257 | values. |