/usr/share/perl5/IO/Async/Routine.pm is in libio-async-perl 0.51-4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 | # You may distribute under the terms of either the GNU General Public License
# or the Artistic License (the same terms as Perl itself)
#
# (C) Paul Evans, 2012 -- leonerd@leonerd.org.uk
package IO::Async::Routine;
use strict;
use warnings;
our $VERSION = '0.51';
use base qw( IO::Async::Process );
use IO::Async::OS;
=head1 NAME
C<IO::Async::Routine> - execute code in an independent sub-process
=head1 SYNOPSIS
use IO::Async::Routine;
use IO::Async::Channel;
use IO::Async::Loop;
my $loop = IO::Async::Loop->new;
my $nums_ch = IO::Async::Channel->new;
my $ret_ch = IO::Async::Channel->new;
my $routine = IO::Async::Routine->new(
channels_in => [ $nums_ch ],
channels_out => [ $ret_ch ],
code => sub {
my @nums = @{ $nums_ch->recv };
my $ret = 0; $ret += $_ for @nums;
# Can only send references
$ret_ch->send( \$ret );
},
on_finish => sub {
say "The routine aborted early - $_[-1]";
$loop->stop;
},
);
$loop->add( $routine );
$nums_ch->send( [ 10, 20, 30 ] );
$ret_ch->recv(
on_recv => sub {
my ( $ch, $totalref ) = @_;
say "The total of 10, 20, 30 is: $$totalref";
$loop->stop;
}
);
$loop->run;
=head1 DESCRIPTION
This subclass of L<IO::Async::Process> contains a body of code and executes it
in a sub-process, allowing it to act independently of the main program. Once
set up, all communication with the code happens by values passed into or out
of the Routine via L<IO::Async::Channel> objects.
Because the code running inside the Routine runs within its own process, it
is isolated from the rest of the program, in terms of memory, CPU time, and
other resources, and perhaps most importantly in terms of control flow. The
code contained within the Routine is free to make blocking calls without
stalling the rest of the program. This makes it useful for using existing code
which has no option not to block within an C<IO::Async>-based program.
To create asynchronous wrappers of functions that return a value based only on
their arguments, and do not generally maintain state within the process it may
be more convenient to use an L<IO::Async::Function> instead, which uses an
C<IO::Async::Routine> to contain the body of the function and manages the
Channels itself.
=cut
=head1 PARAMETERS
The following named parameters may be passed to C<new> or C<configure>:
=over 8
=item channels_in => ARRAY of IO::Async::Channel
ARRAY reference of C<IO::Async::Channel> objects to set up for passing values
in to the Routine.
=item channels_out => ARRAY of IO::Async::Channel
ARRAY reference of C<IO::Async::Channel> objects to set up for passing values
out of the Routine.
=item code => CODE
CODE reference to the body of the Routine, to execute once the channels are
set up.
=back
=cut
sub configure
{
my $self = shift;
my %params = @_;
# TODO: Can only reconfigure when not running
foreach (qw( channels_in channels_out )) {
$self->{$_} = delete $params{$_} if exists $params{$_};
}
$self->SUPER::configure( %params );
}
sub _add_to_loop
{
my $self = shift;
my ( $loop ) = @_;
my @setup;
my @channels_in;
my @channels_out;
foreach my $ch ( @{ $self->{channels_in} || [] } ) {
my ( $rd, $wr );
unless( $rd = $ch->_extract_read_handle ) {
( $rd, $wr ) = IO::Async::OS->pipepair;
}
push @setup, $rd => "keep";
push @channels_in, [ $ch, $wr, $rd ];
}
foreach my $ch ( @{ $self->{channels_out} || [] } ) {
my ( $rd, $wr );
unless( $wr = $ch->_extract_write_handle ) {
( $rd, $wr ) = IO::Async::OS->pipepair;
}
push @setup, $wr => "keep";
push @channels_out, [ $ch, $rd, $wr ];
}
# TODO: This breaks encap.
my $code = delete $self->{code};
my $setup = delete $self->{setup};
push @setup, @$setup if $setup;
$self->configure(
setup => \@setup,
code => sub {
foreach ( @channels_in ) {
my ( $ch, undef, $rd ) = @$_;
$ch->setup_sync_mode( $rd );
}
foreach ( @channels_out ) {
my ( $ch, undef, $wr ) = @$_;
$ch->setup_sync_mode( $wr );
}
my $ret = $code->();
foreach ( @channels_in, @channels_out ) {
my ( $ch ) = @$_;
$ch->close;
}
return $ret;
},
);
foreach ( @channels_in ) {
my ( $ch, $wr ) = @$_;
$ch->setup_async_mode( write_handle => $wr );
$self->add_child( $ch ) unless $ch->parent;
}
foreach ( @channels_out ) {
my ( $ch, $rd ) = @$_;
$ch->setup_async_mode( read_handle => $rd );
$self->add_child( $ch ) unless $ch->parent;
}
$self->SUPER::_add_to_loop( $loop );
foreach ( @channels_in, @channels_out ) {
my ( undef, undef, $other ) = @$_;
$other->close;
}
}
=head1 METHODS
This class provides no additional methods, other than those provided by
L<IO::Async::Process>.
=cut
=head1 AUTHOR
Paul Evans <leonerd@leonerd.org.uk>
=cut
0x55AA;
|