Skip to content

Instantly share code, notes, and snippets.

@exodist
Created September 13, 2014 03:09
Show Gist options
  • Save exodist/c07ddbb490a6ffd5b217 to your computer and use it in GitHub Desktop.
Save exodist/c07ddbb490a6ffd5b217 to your computer and use it in GitHub Desktop.
fork code
sub use_fork {
require Storable;
my ($r, $w);
pipe($r, $w) || confess "Could not open pipe! $!";
$_[0]->[_USE_FORK] ||= [$r, $w];
return 1;
}
sub fork_out {
my $self = shift;
confess "Fork support has not been turned on!" unless $self->[_USE_FORK];
my $wh = $self->[_USE_FORK]->[1];
for my $event (@_) {
next unless $event;
next if $event->isa('Test::Stream::Event::Finish');
# First write the file, then rename it so that it is not read before it is ready.
Storable::store_fd($event, $wh) || confess "Trouble writing event $$ - $!";
}
}
sub fork_cull {
my $self = shift;
confess "fork_cull() can only be called from the parent process!"
if $$ eq $self->[PID];
confess "Fork support has not been turned on!"
unless $self->[_USE_FORK];
my $wh = $self->[_USE_FORK]->[0];
while(my $obj = Storable::fd_retrieve($wh)) {
$self->send($obj);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment