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
    Welcome to the monastery

    What is the question?

    update

    From the comments inside the code, it seems you're asking how to fork in an OS agnostic way.

    On a side note:

    With this chain strategy you are risking to run unlimited numbers of jobs in parallel .

    Having one master controlling all slaves would give you better control.

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

      My apologies for the missing question - updated.

      As for the chain, each time a script is started, an integer is passed as an arg that indicates how many times the script can be spawned. Each spawn decrements the int. "perl spawn.pl 3" will cause 3 instances of the script to be spawned:

      D:\Views Tests>perl spawn.pl 3 Starting PID 3616 as instance 3 Starting PID 5940 as instance 2 Starting PID 6064 as instance 1 PID 3616 ended D:\Views Tests>Starting PID 4564 as instance 0 PID 5940 ended PID 6064 ended PID 4564 ended
        > Each spawn decrements the int.

        Still, you are unnecessarily giving up control, like the possibility to kill the master and all children at the same time.

        see also perlipc

        This is not my field of expertise, but IMHO your requirement not to use extra modules will make this a rather complicated task.

        Cheers Rolf
        (addicted to the Perl Programming Language and ☆☆☆☆ :)
        Je suis Charlie!

Re: Yet another spawn question. Portable method?
by Anonymous Monk on Sep 17, 2017 at 14:16 UTC
    Instead of trying once again to re-invent the wheel, just search CPAN for any one of literally hundreds of modules which are "job runners" or "task runners" or "task schedulers" or what-have-you. The thing that you contemplate doing has already been done to death, such that you need not write anything "from scratch," yourself. "Do not do a thing already done ..."
      Excellent advice. I don't know how many articles, notes, posts etc I've looked at that show how to do this and not a single one of them meet my primary goal: run on any platform without modification. I wrote a sub that runs anywhere and picks the proper system (...) version. Not pretty but it does meet requirements. If anyone wants it, ask.