File: parproc.pl

package info (click to toggle)
libproc-simple-perl 1.32-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 180 kB
  • sloc: perl: 448; makefile: 7
file content (36 lines) | stat: -rwxr-xr-x 1,524 bytes parent folder | download | duplicates (5)
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
#!/usr/local/bin/perl -w
######################################################################
# parproc.pl -- Sample script, runs 10 jobs, 5 at a time.
#
# From the book Perl Power! (Addison-Wesley) by Michael Schilli 1999
######################################################################

use Proc::Simple;

$| = 1;                                 # debuffer output
$max_parallel_jobs = 5;                 # jobs processed in parallel
@running = ();                          # array of running jobs

foreach $job (1..9) {                   # create pseudo jobs
    push(@todo, "sleep 3"); 
}                                       

######################################################################
                                        # while there are jobs to do
while($#todo >= 0 || $#running >= 0) {  # or started ones are running
    @running = grep { $_->poll() } @running;  # remove finished jobs

    if($#running + 1 < $max_parallel_jobs &&  # space free in running?
       defined($job = pop(@todo))) {          # ... and job available

        print "Starting job '$job' ... ";
        $proc = Proc::Simple->new();    # new process
        $proc->start($job) || die "Cannot start job $job";
        push(@running, $proc);          # include in running list
    
        print "STARTED. (Remaining: ", $#todo+1, 
              " Running: ", $#running + 1, ")\n";
        next;                           # proceed without delay
    }
    sleep(1);                           # pause ... and proceed
}