Hello,
I need to write a script that kicks off several processes at once. But between each fork, there should be delay of n seconds. I am using Parallel:ForkManager and know that I have to use sleep but not sure on the how.
#!/usr/software/bin/perl5.8.8 -w
use strict;
use warnings;
use Cwd qw(abs_path);
use Parallel::ForkManager;
my $pm = new Parallel::ForkManager(8);
$pm->run_on_finish(sub {
my($pid, $exit_code) = @_;
my $output = "$pid has finished with exit code $exit_code.\n";
print $output;
});
foreach my $x (0..2) {
$pm->start($x) and next;
sleep(1); # I want to sleep for 2 mins before the next process kic
+ks off.
my $time = time;
print "$x\n";
print "$time\n";
$pm->finish();
}
$pm->wait_all_children;
The above code is wrong because every process sleeps for 2 mins and they resume at the same time. Instead I would like process 0 to start, 2 mins goes by, then process 1 joins, 2 mins go by and then process 3 joins, etc...
Is there someway to do this?
Thanks for your help.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.