blib/lib/IO/Async/Routine.pm

filter: Statementstmt 74.0% 143 / 193 Branchbran 43.0% 31 / 72 Conditioncond 57.6% 15 / 26 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 83.8% 26 / 31 Podpod 100% 5 / 5 totaltotal 64.1% 220 / 343 CC 59 SCAR 24.3
CC
59
Cov
65%
__ANON__
56.0
__ANON__
20.0
configure
18.9
CRAP 11.4
« blib/lib/IO/Async/Process.pm ↑ Summary blib/lib/IO/Async/Handle.pm »
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2012-2024 -- leonerd@leonerd.org.uk
5  
6 package IO::Async::Routine 0.805;
7  
8 12 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 12 ▶use base qw( IO::Async::Notifier );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 12 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 12 ▶use IO::Async::OS;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 12 ▶use IO::Async::Process;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 12 ▶use Struct::Dumb qw( readonly_struct );
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 =head1 NAME
21  
22 C<IO::Async::Routine> - execute code in an independent sub-process or thread
23  
24 =head1 SYNOPSIS
25  
26 =for highlighter language=perl
27  
28 use IO::Async::Routine;
29 use IO::Async::Channel;
30  
31 use IO::Async::Loop;
32 my $loop = IO::Async::Loop->new;
33  
34 my $nums_ch = IO::Async::Channel->new;
35 my $ret_ch = IO::Async::Channel->new;
36  
37 my $routine = IO::Async::Routine->new(
38 channels_in => [ $nums_ch ],
39 channels_out => [ $ret_ch ],
40  
41 code => sub {
42 my @nums = @{ $nums_ch->recv };
43 my $ret = 0; $ret += $_ for @nums;
44  
45 # Can only send references
46 $ret_ch->send( \$ret );
47 },
48  
49 on_finish => sub {
50 say "The routine aborted early - $_[-1]";
51 $loop->stop;
52 },
53 );
54  
55 $loop->add( $routine );
56  
57 $nums_ch->send( [ 10, 20, 30 ] );
58 $ret_ch->recv(
59 on_recv => sub {
60 my ( $ch, $totalref ) = @_;
61 say "The total of 10, 20, 30 is: $$totalref";
62 $loop->stop;
63 }
64 );
65  
66 $loop->run;
67  
68 =head1 DESCRIPTION
69  
70 This L<IO::Async::Notifier> contains a body of code and executes it in a
71 sub-process or thread, allowing it to act independently of the main program.
72 Once set up, all communication with the code happens by values passed into or
73 out of the Routine via L<IO::Async::Channel> objects.
74  
75 The code contained within the Routine is free to make blocking calls without
76 stalling the rest of the program. This makes it useful for using existing code
77 which has no option not to block within an L<IO::Async>-based program.
78  
79 To create asynchronous wrappers of functions that return a value based only on
80 their arguments, and do not generally maintain state within the process it may
81 be more convenient to use an L<IO::Async::Function> instead, which uses an
82 C<IO::Async::Routine> to contain the body of the function and manages the
83 Channels itself.
84  
85 =head2 Models
86  
87 A choice of detachment model is available. Each has various advantages and
88 disadvantages. Not all of them may be available on a particular system.
89  
90 =head3 The C<fork> model
91  
92 The code in this model runs within its own process, created by calling
93 C<fork()> from the main process. It is isolated from the rest of the program
94 in terms of memory, CPU time, and other resources. Because it is started
95 using C<fork()>, the initial process state is a clone of the main process.
96  
97 This model performs well on UNIX-like operating systems which possess a true
98 native C<fork()> system call, but is not available on C<MSWin32> for example,
99 because the operating system does not provide full fork-like semantics.
100  
101 =head3 The C<thread> model
102  
103 The code in this model runs inside a separate thread within the main process.
104 It therefore shares memory and other resources such as open filehandles with
105 the main thread. As with the C<fork> model, the initial thread state is cloned
106 from the main controlling thread.
107  
108 This model is only available on perls built to support threading.
109  
110 =head3 The C<spawn> model
111  
112 I<Since version 0.79.>
113  
114 The code in this model runs within its own freshly-created process running
115 another copy of the perl interpreter. Similar to the C<fork> model it
116 therefore has its own memory, CPU time, and other resources. However, since it
117 is started freshly rather than by cloning the main process, it starts up in a
118 clean state, without any shared resources from its parent.
119  
120 Since this model creates a new fresh process rather than sharing existing
121 state, it cannot use the C<code> argument to specify the routine body; it must
122 instead use only the C<module> and C<func> arguments.
123  
124 In the current implementation this model requires exactly one input channel
125 and exactly one output channel; both must be present, and there cannot be more
126 than one of either.
127  
128 =cut
129  
130 =head1 EVENTS
131  
132 =head2 on_finish $exitcode
133  
134 For C<fork()>-based Routines, this is invoked after the process has exited and
135 is passed the raw exitcode status.
136  
137 =head2 on_finish $type, @result
138  
139 For thread-based Routines, this is invoked after the thread has returned from
140 its code block and is passed the C<on_joined> result.
141  
142 As the behaviour of these events differs per model, it may be more convenient
143 to use C<on_return> and C<on_die> instead.
144  
145 =head2 on_return $result
146  
147 Invoked if the code block returns normally. Note that C<fork()>-based Routines
148 can only transport an integer result between 0 and 255, as this is the actual
149 C<exit()> value.
150  
151 =head2 on_die $exception
152  
153 Invoked if the code block fails with an exception.
154  
155 =cut
156  
157 =head1 PARAMETERS
158  
159 The following named parameters may be passed to C<new> or C<configure>:
160  
161 =head2 model => "fork" | "thread" | "spawn"
162  
163 Optional. Defines how the routine will detach itself from the main process.
164 See the L</Models> section above for more detail.
165  
166 If the model is not specified, the environment variable
167 C<IO_ASYNC_ROUTINE_MODEL> is used to pick a default. If that isn't defined,
168 C<fork> is preferred if it is available, otherwise C<thread>.
169  
170 =head2 channels_in => ARRAY of IO::Async::Channel
171  
172 ARRAY reference of L<IO::Async::Channel> objects to set up for passing values
173 in to the Routine.
174  
175 =head2 channels_out => ARRAY of IO::Async::Channel
176  
177 ARRAY reference of L<IO::Async::Channel> objects to set up for passing values
178 out of the Routine.
179  
180 =head2 code => CODE
181  
182 CODE reference to the body of the Routine, to execute once the channels are
183 set up.
184  
185 When using the C<spawn> model, this is not permitted; you must use C<module>
186 and C<func> instead.
187  
188 =head2 module => STRING
189  
190 =head2 func => STRING
191  
192 I<Since version 0.79.>
193  
194 An alternative to the C<code> argument, which names a module to load and a
195 function to call within it. C<module> should give a perl module name (i.e.
196 C<Some::Name>, not a filename like F<Some/Name.pm>), and C<func> should give
197 the basename of a function within that module (i.e. without the module name
198 prefixed). It will be invoked as the main code body of the object, and passed
199 in a list of all the channels; first the input ones then the output ones.
200  
201 module::func( @channels_in, @channels_out );
202  
203 =head2 setup => ARRAY
204  
205 Optional. For C<fork()>-based Routines, gives a reference to an array to pass
206 to the underlying C<Loop> C<fork_child> method. Ignored for thread-based
207 Routines.
208  
209 =cut
210  
211 12 ▶use constant PREFERRED_MODEL =>
Branch: 2 branches
BranchTrueFalseTotal
'IO::Async::OS'->HAVE_POSIX_FORK ? : 12 0 12
'IO::Async::OS'->HAVE_THREADS ? : 0 0 0
212 IO::Async::OS->HAVE_POSIX_FORK ? "fork" :
213 IO::Async::OS->HAVE_THREADS ? "thread" :
214 12 ▶ die "No viable Routine models";
Subroutine BEGIN: called CC=3 SCAR=11.0
215  
216 sub _init
217 {
218 74 ▶ my $self = shift;
Subroutine _init: called CC=3 SCAR=11.3
219 74 my ( $params ) = @_;
220  
221 74 ▶ $params->{model} ||= $ENV{IO_ASYNC_ROUTINE_MODEL} || PREFERRED_MODEL;
Condition60%3/5
$params->{"model"} ||= $ENV{"IO_ASYNC_ROUTINE_MODEL"} || "fork"
l!l&&r!l&&!r
25490
$ENV{'IO_ASYNC_ROUTINE_MODEL'} || 'fork'
l!l
049
MC/DC0%0/2
$params->{"model"} ||= $ENV{"IO_ASYNC_ROUTINE_MODEL"} || "fork"
$params->{"model"} $ENV{'IO_ASYNC_ROUTINE_MODEL'}
Truth table33%1/3
A ||= B
A= $params->{"model"}B= $ENV{'IO_ASYNC_ROUTINE_MODEL'}
ABresult
1X1
011
000
222  
223 74 $self->SUPER::_init( @_ );
224 }
225  
226 12 my %SETUP_CODE;
227  
228 sub configure
229 {
230 74 ▶ my $self = shift;
Subroutine configure: called CC=14 SCAR=29.4
Pod documented
231 74 my %params = @_;
232  
233 # TODO: Can only reconfigure when not running
234 74 foreach (qw( channels_in channels_out code module func setup on_finish on_return on_die )) {
235 666 ▶ $self->{$_} = delete $params{$_} if exists $params{$_};
Branch: 1 branch
BranchTrueFalseTotal
if exists $params{$_} 448 218 666
236 }
237  
238 defined $self->{code} and defined $self->{func} and
239 74 ▶ croak "Cannot ->configure both 'code' and 'func'";
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{"code"} and defined $self->{"func"} and croak("Cannot ->configure both 'code' and 'func'") 0 74 74
Condition66%2/3
defined $self->{"code"} and defined $self->{"func"}
!ll&&!rl&&r
10640
MC/DC0%0/2
defined $self->{"code"} and defined $self->{"func"}
defined $self->{"code"} defined $self->{"func"}
Truth table66%2/3
A and B
A= defined $self->{"code"}B= defined $self->{"func"}
ABresult
0X0
100
111
240 defined $self->{func} and !defined $self->{module} and
241 74 ▶ croak "'func' parameter requires a 'module' as well";
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{"func"} and not defined $self->{"module"} and croak("'func' parameter requires a 'module' as well") 0 74 74
Condition66%2/3
defined $self->{"func"} and not defined $self->{"module"}
!ll&&!rl&&r
64100
MC/DC0%0/2
defined $self->{"func"} and not defined $self->{"module"}
defined $self->{"func"} not defined $self->{"module"}
Truth table66%2/3
A and not(B)
A= defined $self->{"func"}B= not defined $self->{"module"}
ABresult
0X0
100
111
242  
243 74 ▶ if( defined( my $model = delete $params{model} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $model = delete $params{"model"})) 74 0 74
244 74 ▶ ( $SETUP_CODE{$model} ||= $self->can( "_setup_$model" ) )
Branch: 1 branch
BranchTrueFalseTotal
$SETUP_CODE{$model} ||= $self->can("_setup_$model") or die "Unrecognised Routine model $model" 0 74 74
Condition66%2/3
$SETUP_CODE{$model} ||= $self->can("_setup_$model")
l!l&&r!l&&!r
63110
MC/DC0%0/2
$SETUP_CODE{$model} ||= $self->can("_setup_$model")
$SETUP_CODE{$model} $self->can("_setup_$model")
Truth table66%2/3
A ||= B
A= $SETUP_CODE{$model}B= $self->can("_setup_$model")
ABresult
1X1
011
000
245 or die "Unrecognised Routine model $model";
246  
247 # TODO: optional plugin "configure" check here?
248 74 ▶ $model eq "fork" and !IO::Async::OS->HAVE_POSIX_FORK and
Branch: 1 branch
BranchTrueFalseTotal
$model eq "fork" and not "IO::Async::OS"->HAVE_POSIX_FORK and croak("Cannot use 'fork' model as fork() is not available") 0 74 74
Condition66%2/3
$model eq "fork" and not "IO::Async::OS"->HAVE_POSIX_FORK
!ll&&!rl&&r
4700
MC/DC0%0/2
$model eq "fork" and not "IO::Async::OS"->HAVE_POSIX_FORK
$model eq "fork" not "IO::Async::OS"->HAVE_POSIX_FORK
Truth table66%2/3
A and not(B)
A= $model eq "fork"B= not "IO::Async::OS"->HAVE_POSIX_FORK
ABresult
0X0
100
111
249 croak "Cannot use 'fork' model as fork() is not available";
250 74 ▶ $model eq "thread" and !IO::Async::OS->HAVE_THREADS and
Branch: 1 branch
BranchTrueFalseTotal
$model eq "thread" and not "IO::Async::OS"->HAVE_THREADS and croak("Cannot use 'thread' model as threads are not available") 0 74 74
Condition33%1/3
$model eq "thread" and not "IO::Async::OS"->HAVE_THREADS
!ll&&!rl&&r
7400
MC/DC0%0/2
$model eq "thread" and not "IO::Async::OS"->HAVE_THREADS
$model eq "thread" not "IO::Async::OS"->HAVE_THREADS
Truth table33%1/3
A and not(B)
A= $model eq "thread"B= not "IO::Async::OS"->HAVE_THREADS
ABresult
0X0
100
111
251 croak "Cannot use 'thread' model as threads are not available";
252  
253 74 $self->{model} = $model;
254 }
255  
256 74 $self->SUPER::configure( %params );
257 }
258  
259 sub _add_to_loop
260 {
261 74 ▶ my $self = shift;
Subroutine _add_to_loop: called CC=3 SCAR=11.6
262 74 my ( $loop ) = @_;
263 74 $self->SUPER::_add_to_loop( $loop );
264  
265 74 my $model = $self->{model};
266  
267 74 ▶ my $code = ( $SETUP_CODE{$model} ||= $self->can( "_setup_$model" ) )
Branch: 1 branch
BranchTrueFalseTotal
my $code = ($SETUP_CODE{$model} ||= $self->can("_setup_$model")) or die "Unrecognised Routine model $model" 0 74 74
Condition33%1/3
$SETUP_CODE{$model} ||= $self->can("_setup_$model")
l!l&&r!l&&!r
7400
MC/DC0%0/2
$SETUP_CODE{$model} ||= $self->can("_setup_$model")
$SETUP_CODE{$model} $self->can("_setup_$model")
Truth table33%1/3
A ||= B
A= $SETUP_CODE{$model}B= $self->can("_setup_$model")
ABresult
1X1
011
000
268 or die "Unrecognised Routine model $model";
269  
270 74 $self->$code();
271 }
272  
273 12 readonly_struct ChannelSetup => [qw( chan myfd otherfd )];
274  
275 sub _create_channels_in
276 {
277 74 ▶ my $self = shift;
Subroutine _create_channels_in: called CC=4 SCAR=13.9
278  
279 74 my @channels_in;
280  
281 74 ▶ foreach my $ch ( @{ $self->{channels_in} || [] } ) {
Branch: 1 branch
BranchTrueFalseTotal
$self->{"channels_in"} or [] 9 65 74
282 66 my ( $rd, $wr );
283 66 ▶ unless( $rd = $ch->_extract_read_handle ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rd = $ch->_extract_read_handle) 65 1 66
284 65 ( $rd, $wr ) = IO::Async::OS->pipepair;
285 }
286 66 push @channels_in, ChannelSetup( $ch, $wr, $rd );
287 }
288  
289 74 return @channels_in;
290 }
291  
292 sub _create_channels_out
293 {
294 74 ▶ my $self = shift;
Subroutine _create_channels_out: called CC=4 SCAR=13.9
295  
296 74 my @channels_out;
297  
298 74 ▶ foreach my $ch ( @{ $self->{channels_out} || [] } ) {
Branch: 1 branch
BranchTrueFalseTotal
$self->{"channels_out"} or [] 8 66 74
299 67 my ( $rd, $wr );
300 67 ▶ unless( $wr = $ch->_extract_write_handle ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($wr = $ch->_extract_write_handle) 67 0 67
301 67 ( $rd, $wr ) = IO::Async::OS->pipepair;
302 }
303 67 push @channels_out, ChannelSetup( $ch, $rd, $wr );
304 }
305  
306 74 return @channels_out;
307 }
308  
309 sub _adopt_channels_in
310 {
311 74 ▶ my $self = shift;
Subroutine _adopt_channels_in: called CC=3 SCAR=11.0
312 74 my ( @channels_in ) = @_;
313  
314 74 foreach ( @channels_in ) {
315 66 my $ch = $_->chan;
316 66 $ch->setup_async_mode( write_handle => $_->myfd );
317 66 ▶ $self->add_child( $ch ) unless $ch->parent;
Branch: 1 branch
BranchTrueFalseTotal
unless $ch->parent 65 1 66
318 }
319 }
320  
321 sub _adopt_channels_out
322 {
323 74 ▶ my $self = shift;
Subroutine _adopt_channels_out: called CC=3 SCAR=11.0
324 74 my ( @channels_out ) = @_;
325  
326 74 foreach ( @channels_out ) {
327 67 my $ch = $_->chan;
328 67 $ch->setup_async_mode( read_handle => $_->myfd );
329 67 ▶ $self->add_child( $ch ) unless $ch->parent;
Branch: 1 branch
BranchTrueFalseTotal
unless $ch->parent 67 0 67
330 }
331 }
332  
333 sub _setup_fork
334 {
335 70 ▶ my $self = shift;
Subroutine _setup_fork: called CC=3 SCAR=11.4
336  
337 70 my @channels_in = $self->_create_channels_in;
338 70 my @channels_out = $self->_create_channels_out;
339  
340 70 my $code = $self->{code};
341  
342 70 my $module = $self->{module};
343 70 my $func = $self->{func};
344  
345 70 my @setup = map { $_->otherfd => "keep" } @channels_in, @channels_out;
346  
347 70 my $setup = $self->{setup};
348 70 ▶ push @setup, @$setup if $setup;
Branch: 1 branch
BranchTrueFalseTotal
if $setup 1 69 70
349  
350 my $process = IO::Async::Process->new(
351 setup => \@setup,
352 code => sub {
353 1 ▶ foreach ( @channels_in, @channels_out ) {
Subroutine __ANON__: called CC=5 SCAR=23.4
354 2 $_->chan->setup_sync_mode( $_->otherfd );
355 }
356  
357 1 ▶ if( defined $module ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $module) 0 1 1
358 0 ( my $file = "$module.pm" ) =~ s{::}{/}g;
359 0 require $file;
360  
361 0 ▶ $code = $module->can( $func ) or
Branch: 1 branch
BranchTrueFalseTotal
$code = $module->can($func) or die "Module '${module}' has no '${func}'\n" 0 0 0
362 die "Module '$module' has no '$func'\n";
363 }
364  
365 1 my $ret = $code->( map { $_->chan } @channels_in, @channels_out );
366  
367 0 foreach ( @channels_in, @channels_out ) {
368 0 $_->chan->close;
369 }
370  
371 0 return $ret;
372 },
373 on_finish => $self->_replace_weakself( sub {
374 9 ▶ my $self = shift or return;
Subroutine __ANON__: called CC=3 SCAR=11.2
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or return 0 9 9
375 9 my ( $exitcode ) = @_;
376 9 $self->maybe_invoke_event( on_finish => $exitcode );
377  
378 9 ▶ unless( $exitcode & 0x7f ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($exitcode & 127) 9 0 9
379 9 $self->maybe_invoke_event( on_return => ($exitcode >> 8) );
380 9 $self->result_future->done( $exitcode >> 8 );
381 }
382 }),
383 on_exception => $self->_replace_weakself( sub {
384 4 ▶ my $self = shift or return;
Subroutine __ANON__: called CC=2 SCAR=7.0
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or return 0 4 4
385 4 my ( $exception, $errno, $exitcode ) = @_;
386  
387 4 $self->maybe_invoke_event( on_die => $exception );
388 4 $self->result_future->fail( $exception, routine => );
389 70 }),
390 );
391  
392 70 $self->_adopt_channels_in ( @channels_in );
393 70 $self->_adopt_channels_out( @channels_out );
394  
395 70 $self->add_child( $self->{process} = $process );
396 69 $self->{id} = "P" . $process->pid;
397  
398 69 $_->otherfd->close for @channels_in, @channels_out;
399 }
400  
401 sub _setup_thread
402 {
403 0 ▶ my $self = shift;
Subroutine _setup_thread: not called CC=2 SCAR=17.9
404  
405 0 my @channels_in = $self->_create_channels_in;
406 0 my @channels_out = $self->_create_channels_out;
407  
408 0 my $code = $self->{code};
409  
410 0 my $module = $self->{module};
411 0 my $func = $self->{func};
412  
413 my $tid = $self->loop->create_thread(
414 code => sub {
415 0 ▶ foreach ( @channels_in, @channels_out ) {
Subroutine __ANON__: not called CC=7 SCAR=40.3
416 0 $_->chan->setup_sync_mode( $_->otherfd );
417 0 ▶ defined and $_->close for $_->myfd;
Branch: 1 branch
BranchTrueFalseTotal
defined $_ and $_->close 0 0 0
418 }
419  
420 0 ▶ if( defined $func ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $func) 0 0 0
421 0 ( my $file = "$module.pm" ) =~ s{::}{/}g;
422 0 require $file;
423  
424 0 ▶ $code = $module->can( $func ) or
Branch: 1 branch
BranchTrueFalseTotal
$code = $module->can($func) or die "Module '${module}' has no '${func}'\n" 0 0 0
425 die "Module '$module' has no '$func'\n";
426 }
427  
428 0 my $ret = $code->( map { $_->chan } @channels_in, @channels_out );
429  
430 0 foreach ( @channels_in, @channels_out ) {
431 0 $_->chan->close;
432 }
433  
434 0 return $ret;
435 },
436 on_joined => $self->_capture_weakself( sub {
437 0 ▶ my $self = shift or return;
Subroutine __ANON__: not called CC=4 SCAR=30.0
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or return 0 0 0
438 0 my ( $ev, @result ) = @_;
439 0 $self->maybe_invoke_event( on_finish => @_ );
440  
441 0 ▶ if( $ev eq "return" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ev eq "return") 0 0 0
442 0 $self->maybe_invoke_event( on_return => @result );
443 0 $self->result_future->done( @result );
444 }
445 0 ▶ if( $ev eq "died" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ev eq "died") 0 0 0
446 0 $self->maybe_invoke_event( on_die => $result[0] );
447 0 $self->result_future->fail( $result[0], routine => );
448 }
449  
450 0 delete $self->{tid};
451 0 }),
452 );
453  
454 0 $self->{tid} = $tid;
455 0 $self->{id} = "T" . $tid;
456  
457 0 $self->_adopt_channels_in ( @channels_in );
458 0 $self->_adopt_channels_out( @channels_out );
459  
460 0 $_->otherfd->close for @channels_in, @channels_out;
461 }
462  
463 # The injected program that goes into spawn mode
464 12 ▶use constant PERL_RUNNER => <<'EOF';
Subroutine BEGIN: called CC=1 SCAR=0.0
465 ( my ( $module, $func ), @INC ) = @ARGV;
466 ( my $file = "$module.pm" ) =~ s{::}{/}g;
467 require $file;
468 my $code = $module->can( $func ) or die "Module '$module' has no '$func'\n";
469 require IO::Async::Channel;
470 exit $code->( IO::Async::Channel->new_stdin, IO::Async::Channel->new_stdout );
471 EOF
472  
473 sub _setup_spawn
474 {
475 4 ▶ my $self = shift;
Subroutine _setup_spawn: called CC=5 SCAR=20.7
476  
477 $self->{code} and
478 4 ▶ die "Cannot run IO::Async::Routine in 'spawn' with code\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"code"} and die "Cannot run IO::Async::Routine in 'spawn' with code\n" 0 4 4
479  
480 4 ▶ @{ $self->{channels_in} } == 1 or
Branch: 1 branch
BranchTrueFalseTotal
@{$self->{"channels_in"};} == 1 or die "IO::Async::Routine in 'spawn' mode requires exactly one input channel\n" 0 4 4
481 die "IO::Async::Routine in 'spawn' mode requires exactly one input channel\n";
482 4 ▶ @{ $self->{channels_out} } == 1 or
Branch: 1 branch
BranchTrueFalseTotal
@{$self->{"channels_out"};} == 1 or die "IO::Async::Routine in 'spawn' mode requires exactly one output channel\n" 0 4 4
483 die "IO::Async::Routine in 'spawn' mode requires exactly one output channel\n";
484  
485 4 my @channels_in = $self->_create_channels_in;
486 4 my @channels_out = $self->_create_channels_out;
487  
488 4 my $module = $self->{module};
489 4 my $func = $self->{func};
490  
491 my $process = IO::Async::Process->new(
492 setup => [
493 stdin => $channels_in[0]->otherfd,
494 stdout => $channels_out[0]->otherfd,
495 ],
496 32 command => [ $^X, "-E", PERL_RUNNER, $module, $func, grep { !ref } @INC ],
497 on_finish => $self->_replace_weakself( sub {
498 0 ▶ my $self = shift or return;
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or return 0 0 0
499 0 my ( $exitcode ) = @_;
500 0 $self->maybe_invoke_event( on_finish => $exitcode );
501  
502 0 ▶ unless( $exitcode & 0x7f ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($exitcode & 127) 0 0 0
503 0 $self->maybe_invoke_event( on_return => ($exitcode >> 8) );
504 0 $self->result_future->done( $exitcode >> 8 );
505 }
506 }),
507 on_exception => $self->_replace_weakself( sub {
508 0 ▶ my $self = shift or return;
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or return 0 0 0
509 0 my ( $exception, $errno, $exitcode ) = @_;
510  
511 0 $self->maybe_invoke_event( on_die => $exception );
512 0 $self->result_future->fail( $exception, routine => );
513 4 }),
514 );
515  
516 4 $self->_adopt_channels_in ( @channels_in );
517 4 $self->_adopt_channels_out( @channels_out );
518  
519 4 $self->add_child( $self->{process} = $process );
520 2 $self->{id} = "P" . $process->pid;
521  
522 2 $_->otherfd->close for @channels_in, @channels_out;
523 }
524  
525 =head1 METHODS
526  
527 =cut
528  
529 =head2 id
530  
531 $id = $routine->id;
532  
533 Returns an ID string that uniquely identifies the Routine out of all the
534 currently-running ones. (The ID of already-exited Routines may be reused,
535 however.)
536  
537 =cut
538  
539 sub id
540 {
541 101 ▶ my $self = shift;
Subroutine id: called CC=1 SCAR=0.0
Pod documented
542 101 return $self->{id};
543 }
544  
545 =head2 model
546  
547 $model = $routine->model;
548  
549 Returns the detachment model in use by the Routine.
550  
551 =cut
552  
553 sub model
554 {
555 2 ▶ my $self = shift;
Subroutine model: called CC=1 SCAR=0.0
Pod documented
556 2 return $self->{model};
557 }
558  
559 =head2 kill
560  
561 $routine->kill( $signal );
562  
563 Sends the specified signal to the routine code. This is either implemented by
564 C<CORE::kill()> or C<threads::kill> as required. Note that in the thread case
565 this has the usual limits of signal delivery to threads; namely, that it works
566 at the Perl interpreter level, and cannot actually interrupt blocking system
567 calls.
568  
569 =cut
570  
571 sub kill
572 {
573 2 ▶ my $self = shift;
Subroutine kill: called CC=3 SCAR=11.4
Pod documented
574 2 my ( $signal ) = @_;
575  
576 2 ▶ $self->{process}->kill( $signal ) if $self->{model} eq "fork";
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"model"} eq "fork" 2 0 2
577 2 ▶ threads->object( $self->{tid} )->kill( $signal ) if $self->{model} eq "thread";
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"model"} eq "thread" 0 2 2
578 }
579  
580 =head2 result_future
581  
582 $f = $routine->result_future;
583  
584 I<Since version 0.75.>
585  
586 Returns a new C<IO::Async::Future> which will complete with the eventual
587 return value or exception when the routine finishes.
588  
589 If the routine finishes with a successful result then this will be the C<done>
590 result of the future. If the routine fails with an exception then this will be
591 the C<fail> result.
592  
593 =cut
594  
595 sub result_future
596 {
597 20 ▶ my $self = shift;
Subroutine result_future: called CC=2 SCAR=7.0
Pod documented
598  
599 20 ▶ return $self->{result_future} //= do {
Condition66%2/3
$self->{"result_future"} //= do { my $f = $self->loop->new_future; $f->on_ready(sub { undef $self; } ); $f }
l!l&&r!l&&!r
7130
MC/DC0%0/2
$self->{"result_future"} //= do { my $f = $self->loop->new_future; $f->on_ready(sub { undef $self; } ); $f }
$self->{"result_future"} do { my $f = $self->loop->new_future; $f->on_ready(sub { undef $self; } ); $f }
Truth table66%2/3
A //= B
A= $self->{"result_future"}B= do { my $f = $self->loop->new_future; $f->on_ready(sub { undef $self; } ); $f }
ABresult
1X1
011
000
600 13 my $f = $self->loop->new_future;
601 # This future needs to strongly retain $self to ensure it definitely gets
602 # notified
603 13 ▶ $f->on_ready( sub { undef $self } );
Subroutine __ANON__: called CC=1 SCAR=0.0
604 13 $f;
605 };
606 }
607  
608 =head1 AUTHOR
609  
610 Paul Evans <leonerd@leonerd.org.uk>
611  
612 =cut
613  
614 12 0x55AA;
« blib/lib/IO/Async/Process.pm ↑ Summary blib/lib/IO/Async/Handle.pm »