MickeyLane has asked for the wisdom of the Perl Monks concerning the following question:
I know that this question has been asked 500 times over the last dozen or more years and I've read most of the answers. Since I'm writing this, the answers don't do what I need. Or I haven't found the One True Answer.
Design goal #1: Write a Perl script that runs on any platform that supports Perl without special casing.
Design goal #2: Use the minimum number of manually added packages as possible.
The script runs through a list of possible jobs to do. When it (A) finds one, it 'locks' that job and spawns another instance of the script (B) to continue looking for jobs. When A finishes, it unlocks the job and dies. B becomes the only instance running (the new A). Lather, rinse, repeat.
Question: What is a single platform independent way of spawning another instance of the running script?
The code below needs to be saved as "spawn.pl"
#!/usr/bin/perl use warnings; use strict; use Proc::Background; my $arg = shift @ARGV; if (!defined ($arg)) { print ("Usage: \"perl spawn.pl n\" where n is the number of times +to spawn\n"); exit (0); } my $c = $arg + 0; print ("Starting PID $$ as instance $c\n"); if ($c > 0) { $c--; sleep (2); if (1) { # # This only works on Windows (as is well noted) # system (1, "perl spawn.pl $c"); } elsif (0) { # # This does not work on Windows because it doesn't have the fu +ll path # Same on Linux Mint (Ubuntu or Debian) # my $arg1 = "$c"; # my $command = "perl spawn.pl"; my $command = "spawn.pl"; my $proc1 = Proc::Background->new ($command, $arg1); } elsif (0) { # # Does not work on Windows because it waits for completion # Works on Linux Mint # system ("perl spawn.pl $c &"); } else { # # Need something here # } } sleep (3); print ("PID $$ ended\n"); exit (0);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Yet another spawn question. Portable method?
by Corion (Patriarch) on Sep 17, 2017 at 07:26 UTC | |
|
Re: Yet another spawn question. Portable method?
by LanX (Saint) on Sep 16, 2017 at 13:28 UTC | |
by MickeyLane (Initiate) on Sep 16, 2017 at 15:20 UTC | |
by LanX (Saint) on Sep 16, 2017 at 15:25 UTC | |
|
Re: Yet another spawn question. Portable method?
by Anonymous Monk on Sep 17, 2017 at 14:16 UTC | |
by MickeyLane (Initiate) on Sep 20, 2017 at 18:05 UTC |