Skip to content

Commit

Permalink
- Added the Block.pfork() method which works almost identical with th…
Browse files Browse the repository at this point in the history
…e Perl fork() function -- it doesn't create a temporary file with the result of the computation, but it still returns a Fork object.

Example:
	var f = { say "hello" }.pfork;
	f.wait;
  • Loading branch information
trizen committed Jul 20, 2015
1 parent 8fdb831 commit 24e928e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
16 changes: 16 additions & 0 deletions lib/Sidef/Types/Block/Code.pm
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,22 @@ package Sidef::Types::Block::Code {
$fork;
}

sub pfork {
my ($self) = @_;

my $fork = Sidef::Types::Block::Fork->new();

my $pid = fork() // die "[FATAL ERROR]: cannot fork";
if ($pid == 0) {
srand();
$self->run;
exit 0;
}

$fork->{pid} = $pid;
$fork;
}

sub thread {
my ($self) = @_;
state $x = do {
Expand Down
7 changes: 4 additions & 3 deletions lib/Sidef/Types/Block/Fork.pm
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ package Sidef::Types::Block::Fork {
sub get {
my ($self) = @_;

exists($self->{result})
or return;

# Wait for the process to finish
waitpid($self->{pid}, 0);

# Return when the fork doesn't hold a result
exists($self->{result})
or return;

state $x = require Storable;
my $ref = eval { Storable::retrieve($self->{result}) };
unlink(delete $self->{result});
Expand Down

0 comments on commit 24e928e

Please sign in to comment.