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

Hi, I'm new to the forums so let me first say "What's up?" and "thank you for looking at my questions!"

Here is what I am doing, I need to launch a separate process which basically just launches a another perl script (.pl not .pm ) to do some transaction based actions in the background while my 'parent' process checks the PID of the child to determine if the execution has finished. This also works to update the 'completion bar' from 0% -> 100% so that the user can see keep track of the progress of the transaction. All this code is existing I've just picked this project up recently so I don't want to do a complete rewrite.

What the application was using previously was the Win32::Spawn module to create the process and basically "fork" returning the PID of the child so that the parent can keep track of the childs progress. However, I've had to add the ability of the child process to execute an external binary and the process that is created by Win32::Spawn does not have the ability to run embedded processes. I've tried everything, exec(), system(), `myexe.exe -args` and nothing works. Trust me, I've even tried simple things like running an helloworld binary from the child process created with Win32::Spawn and it does not run. So far this is what I have used to create my process with Win32::Process

Win32::Process::Create($saSyncPL,"${^X}","$cmdline ${optfile}",0,NORMA +L_PRIORITY_CLASS,"." ) || die "Failed to create commit / sync process + ${^X} ${cmdline}"; $pid=$saSyncPL->GetProcessID(); Log::AvLog("high", "PID = $pid", $Log::LOG_CALLER_NAME);

It looks like my parent is waiting for the child process to finish before it's moving on to other execution. I do not see my "PID = <process id of the child>" being outputted in the log file. Does anyone know what I can do so that this process will launch in the 'background' so to speak? So that my parent will execute in parallel to the child process?

Any and all help is appreciated. Thanks!

Andy

Replies are listed 'Best First'.
Re: Win32::Process - need help from an expert!
by ikegami (Patriarch) on Sep 13, 2007 at 15:36 UTC

    As you can see, Win32::Process::Create does not wait for the child to execute.

    # parent.pl use strict; use warnings; use Win32::Process qw( NORMAL_PRIORITY_CLASS INFINITE ); { Win32::Process::Create( my $child, $^X, "perl child.pl", 0, NORMAL_PRIORITY_CLASS, "." ) or die("Unable to launch child: $^E\n"); print("Lanched child.\n"); while (!$child->Wait(50)) { print("Child still executing...\n"); } $child->GetExitCode(my $child_exit_code); print("Child exited with code $child_exit_code\n"); }
    # child.pl use Time::HiRes qw( sleep ); print "Doing something\n"; sleep(0.100); print "Doing something\n"; sleep(0.100); print "Doing something\n"; sleep(0.100); print "done.\n";
    >perl parent.pl Lanched child. Child still executing... Doing something Child still executing... Doing something Child still executing... Child still executing... Doing something Child still executing... Child still executing... done. Child exited with code 0

    Hum, Win32::Process has a very un-perl-like interface. (It returns values through arguments.) And you can't distinguish whether Wait failed or simply timed out.

Re: Win32::Process - need help from an expert!
by cdarke (Prior) on Sep 13, 2007 at 14:42 UTC
    Your second argument to Win32::Process::Create is a bit strange, it should be the full path name of the child program (the others look OK). It should not wait for the child to complete, use $saSyncPL->Wait(INFINITE); for that.
    I am not sure what you mean by "embedded processes". The UNIX ability to switch programs without creating a new process (exec) is not available on Windows, the perl exec is only pretending to act like the UNIX one.
      The second argument ( $^X ) is the fully qualified path to perl.exe. I'll get rid of that for now and use what I know is the fully qualified path and see if it helps. I'll also use the Wain(INFINATE) that you mentioned to see if that helps as well. Thanks for the quick reply! Andy
        Sorry, you are correct, $^X should have worked (I missed the fact that you were running another perl script).
        Is the second script actually running?
Re: Win32::Process - need help from an expert!
by ikegami (Patriarch) on Sep 13, 2007 at 15:12 UTC

    I do not see my "PID = <process id of the child>" being outputted in the log file.

    Perhaps you are Suffering from buffering.

      I am seeing the PID in the logfile now but that's the last thing I see. I should be seeing may other log messages coming from the child process but I'm not so it leads me to believe that the child process isn't actually doing anything.

      I'll read that article though, thank you.

      Andy