Process::Delegatable - Run a Process::Storable object in an isolated perl


Process documentation Contained in the Process distribution.

Index


Code Index:

NAME

Top

Process::Delegatable - Run a Process::Storable object in an isolated perl

SYNOPSIS

Top

  # Background a finite length process
  MyBigMemoryProcess->new( ... )->delegate('/opt/perl5005/perl');

DESCRIPTION

Top

Process::Delegatable extends Process::Storable for the purpose of creating process objects that can be run inside other instances, or even other versions, of Perl, with the results of the object ending up in the object in the current perl instance as if it had been run here.

It adds a delegate method that launches a new copy of Perl, passing the Process to it using Process::Launcher, which will load and execute the process.

Please note that STDERR for the delegated process will be sent to /dev/null (or your equivalent) so there is no way to recieve any error output from the delegated process.

If you want to add logging or locks or some other feature to your delegated process, that is your responsibility, and you set them up in the prepare method.

METHODS

Top

This method inherits all the normal methods from Process and in addition inherits the strict seperation of responsibility described in Process::Storable. You should be aware of both of these.

delegate

  $process->delegate('/opt/perl5004/perl', '-MFoo::Bar');

The delegate method is provided by default, and will start another instance of another Perl (or if no arguments, the same Perl you are current running), although not necesarily with the same environment.

This allows you to use Process::Delegatable to run Processes that may need to allocate a large amount of memory, or need to be rigourously seperated from your main program.

SUPPORT

Top

Bugs should be reported via the CPAN bug tracker at

http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Process

For other issues, contact the author.

AUTHOR

Top

Adam Kennedy <adamk@cpan.org>

SEE ALSO

Top

http://ali.as/

COPYRIGHT

Top


Process documentation Contained in the Process distribution.

package Process::Delegatable;

use 5.00503;
use strict;
use File::Temp        ();
use IPC::Run3         ();
use Probe::Perl       ();
use Storable          ();
use Process::Storable ();

use vars qw{$VERSION @ISA @PERLCMD};
BEGIN {
	$VERSION = '0.28';
	@ISA     = 'Process::Storable';

	# Contains the command to use to launch perl
	# Should be the path to the perl current running.
	# People with special needs should localise this
	# to add any flags.
	@PERLCMD = ( Probe::Perl->find_perl_interpreter );
}

sub delegate {
	my $self  = shift;
	my $class = ref($self);
	my @perl  = @_ ? @_ : @PERLCMD;

	# Dump the object to the input filehandle,
	# and recover it afterwards
	my $stdin  = File::Temp::tempfile();
	my $stdout = File::Temp::tempfile();
	$self->serialize( $stdin );
	seek( $stdin, 0, 0 );

	# Generate the command
	my $cmd = [ @perl, '-MProcess::Launcher', '-e serialized', $class ];

	# Fire the command
	IPC::Run3::run3( $cmd, $stdin, $stdout, \undef );

	# Get the first line with content of the response, which will be an OK/FAIL
	seek( $stdout, 0, 0 );
	my $result;
	while ( 1 ) {
		$result = <$stdout>;
		$result = "FAIL No output returned\n" unless defined $result;
		next unless $result =~ /\S/;
		chomp($result);
		last;
	}
	if ( $result eq 'OK' ) {
		# Looks good, deserialize the data
		my $complete = $class->deserialize( $stdout );
		%$self = %$complete;

		# Clean up and return
		close( $stdin  );
		close( $stdout );
		return 1;
	} else {
		# Just clean up
		close( $stdin  );
		close( $stdout );
	}

	# Is it an error?
	if ( $result =~ s/^FAIL// ) {
		# Failed
		$self->{errstr} = $result;
		return 1;
	}

	# err...
	die "Unknown delegate response $result";
}

1;

__END__