Last active
August 29, 2015 14:21
-
-
Save lukeorland/ec087222e0a834af8211 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
# References: | |
# - http://stackoverflow.com/a/800105/492631 | |
use warnings; | |
use strict; | |
# fork processes | |
my $num_cores = 4; | |
my @children = (); | |
foreach my $core (1..$num_cores) { | |
my $child = fork(); | |
if (! $child) { # I am child | |
print STDERR "running child $core\n"; | |
exec("sleep 2 && echo $core > $core"); | |
exit 0; | |
} | |
push @children, $child; | |
next; | |
} | |
print "waiting on children\n"; | |
foreach my $child (@children) { | |
waitpid( $child, 0 ); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
# References: | |
# - http://stackoverflow.com/a/800105/492631 | |
use warnings; | |
use strict; | |
# fork processes | |
my $num_cores = 4; | |
my $num_jobs = 15; | |
my @children = (); | |
my $job = 0; | |
foreach my $core (1..$num_cores) { | |
if ($job < $num_jobs) { | |
my $child = fork(); | |
if (! $child) { # I am child | |
#print STDERR "running child $core\n"; | |
exec("sleep 1 && echo $job > $job"); | |
exit 0; | |
} | |
push @children, $child; | |
$job = $job + 1; | |
next; | |
} | |
} | |
while (@children) { | |
my $old_child = shift @children; | |
waitpid( $old_child, 0 ); | |
print "child finished\n"; | |
if ($job < $num_jobs) { | |
my $new_child = fork(); | |
if (! $new_child) { # I am child | |
exec("sleep 1 && echo $job > $job"); | |
exit 0; | |
} | |
$job = $job + 1; | |
push @children, $new_child; | |
} | |
} |
Great! What about if you have more jobs than cores? In that case we need to start up next jobs as spots become available.
So parajobs.pl
uses a FIFO queue to run through all the jobs. It's not as optimal as a pool, since some jobs might finish before a job ahead of them.
I tested this, and 15 1-second jobs with 4 cores took 4.226 total seconds.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The output of running this:
so it took 2 seconds total, and each subprocess took 2 seconds.