johnd2323 has asked for the wisdom of the Perl Monks concerning the following question:

greetings perl monks and mavens, Is it truly possible to spawn a child process in perl on windows so that the parent process does NOT wait for the child process to finish before the parent continues its own processing? I’ve tried to accomplish this over the last couple of days using exec, system, wind32::process, proc::background, fork, and more, on windows server 2008 using perl 5.22.1. For each of these statements I’m able to spawn a child process that runs a .bat file successfully, but in each case the parent waits for the .bat file to finish processing ( it takes about 40 seconds ) before the parent proceeds. I’m beginning to think that this is not really possible on windows. I have found dozens of sites that detail usage of each of these statements ( including several perlmonks pages ) and I’ve tried them all. I can get all of the above-listed statements to launch and complete a child process but I can’t get the parent to NOT wait for completion. Any guidance is very gratefully appreciated. Thank you. - john

Replies are listed 'Best First'.
Re: parent not waiting for child process
by BrowserUk (Patriarch) on Aug 02, 2016 at 19:20 UTC

    Yes. Many ways, but the simplest, cleanest and most reliable is: system 1, $cmd, @args;.

    Apparently this is documented in http://perldoc.perl.org/perlport.html; but all you really need to know is that it works the same as system without 1 as a first arg, except:

    1. It doesn't wait for the command to complete.
    2. It returns the pid of the process that is started.

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: parent not waiting for child process
by dasgar (Priest) on Aug 02, 2016 at 20:45 UTC

    If you're wanting to spawn a process and continue on without waiting plus collect data from the spawned process later, you could use threads. To do that, just start a thread that does the background process and your main code will continue on without waiting. And then you can join the thread(s) later to collect their return data.

    However, if you're just wanting to spawn a process and are never concerned about ever checking on it, BrowserUk provided one method for doing so. I was not aware of that method, but I have successfully leveraged the start command available from the command prompt environment. It basically starts a new command prompt to run the desired command and then immediately returns. You can open a command prompt and type start /? to get details about the options for the start command.

Re: parent not waiting for child process
by stevieb (Canon) on Aug 02, 2016 at 18:43 UTC

    The following seems to work for me... the test.bat script simply contains echo "inside batch script":

    Update: added wait() call and associated output. Note that without wait(), child procs could become zombies. The call to wait() can be at the very bottom of the script, inside of a parent block.

    use warnings; use strict; my $pid = fork(); if ($pid){ print "parent...\n"; } else { sleep 2; system "test.bat"; } if ($pid){ print "parent: doing stuff...\n"; } else { print "child: exiting\n"; } if ($pid){ print "parent: done doing stuff, waiting...\n"; wait; print "parent: children dead, safe to exit\n"; }

    Output:

    parent: doing stuff parent: done doing stuff, waiting... "inside batch script" child: exiting parent: children dead, safe to exit

      Using fork emulation on windows is just asking for trouble.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
      In the absence of evidence, opinion is indistinguishable from prejudice.