in reply to Re^2: Win32::Job::watch vs. system(1,...)
in thread Win32::Job::watch vs. system(1,...)

Let's see if I got this right.

  1. You have one process that starts a second asynchronous process (that may start other child processes).
  2. And once it has started that first process, you want it to both watch for the creation of a killfile (produced by yet another asynchronous process).
  3. And at the same time, be able to continue doing other things concurrently with watching for this kill file so that it can kill the process tree it started.

I guess the author would say: I didn't design the watch method for that scenario.


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".
In the absence of evidence, opinion is indistinguishable from prejudice.
RIP PCW It is as I've been saying!(Audio until 20090817)
  • Comment on Re^3: Win32::Job::watch vs. system(1,...)

Replies are listed 'Best First'.
Re^4: Win32::Job::watch vs. system(1,...)
by rovf (Priest) on Sep 25, 2009 at 09:52 UTC
    Basically you are right, but a bit simpler. I want to start a process (let's call it P), and this process might create any number of childs and grandchilds. After the process is started, my program is continuing to do other stuff. From time to time (when my program has nothing else to do, which is typically every few seconds), my program checks whether P is supposed to be killed, or whether it is allowed to continue running, or whether maybe it already has ended. This decision is done by looking whether a certain file exists (this file is created from a different side - from the viewpoint of my program, I only need to see whether the file exists or not). If this certain file exists, and P is still running, my program has to kill P and all its decendents.

    Here the whole thing in pseudocode:

    Create new process P while(1) { read next task process task if(P has ended) { evaluate result of P Create new process P } elsif(P should be killed) { kill P and its children Create new process P } }
    In my original solution, 'create new process' was done with system(1,...) and killing was done with kill -9.

    -- 
    Ronald Fischer <ynnor@mm.st>

      Then watch() is not a good fit for your requirements. All you need is something like:

      my $job = Win32::Job->new; my $pid = $job->spawn(); while( 1 ) { ## read and process next task sleep 3 while $job->status->{ $pid }{ exitcode } == $STILL_RUNNING + ## 259?? and not -e 'killfile'; $job->kill if $job->status->{ $pid }{ exitcode } == $STILL_RUNNING +; $job = Win32::Job->new; $pid = $job->spawn( ... ) }

      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".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        my $pid = $job->spawn();

        I must admit that there is something I don't understand in the documentation of Win32::Job::spawn. The perldoc says:

        spawn($exe, $args, \%opts); Creates a new process and associates it with the Job. The proc +ess is initially suspended, and can be resumed with one of the other methods.
        I was thinking that this means, spawn creates a new process, but does not actually execute it, and that I would have to execute it with one of the other methods (run or watch). In your example, you use neither of these. How is this supposed to work, respectively, in what way did I misunderstand the documentation?

        -- 
        Ronald Fischer <ynnor@mm.st>