rovf has asked for the wisdom of the Perl Monks concerning the following question:
In a recent thread about starting detached processes on Windows using the special form system(1,...), I got a recommendation to use Win32::Job instead of system(1,...), as this would be safer to use and easy too. So I started to play around with Win32::Job, basically trying this:
The purpose of watch() is to abort the spawned program and all child processes it might have been created, prematurely if the file killfile.txt gets created.... my $j=Win32::Job->new; $j->spawn('perl.exe', "perl.exe myprog.pl", { new_console=>0, new_group=>1, stderr=> $out, stdout=> $out }) unlink 'killfile.txt'; $j->watch(sub { -e 'killfile.txt' ? 1 : 0 }, 3); print "go on\n"; ...
This works indeed, but there is one big difference compared to when I implement the same using system(1,...):
With system(1,...), I get a separate process, i.e. my Perl program continues to run. With Win32::Job, the watch call blocks, i.e. "go on" is printed only after "perl.exe myprog.pl" has (forcefully or gracefully) finished; so Win32::Job doesn't seem to be an alternative for system(1,...), but simply something different.
Of course I could combine the two approaches: Write a helper script 'spawn_and_watch.pl', which I would start with system(1,...), and which does the spawn and watch calls; but I have somehow the feeling that there should be an easier solution. Did I miss something here?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Win32::Job::watch vs. system(1,...)
by BrowserUk (Patriarch) on Sep 24, 2009 at 16:38 UTC | |
by rovf (Priest) on Sep 25, 2009 at 07:35 UTC | |
by BrowserUk (Patriarch) on Sep 25, 2009 at 09:28 UTC | |
by rovf (Priest) on Sep 25, 2009 at 09:52 UTC | |
by BrowserUk (Patriarch) on Sep 25, 2009 at 17:51 UTC | |
| |
|
Re: Win32::Job::watch vs. system(1,...)
by Anonymous Monk on Sep 24, 2009 at 14:26 UTC | |
by rovf (Priest) on Sep 25, 2009 at 07:34 UTC |