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

I've written a program specifically to keep batches of X processes running concurrently. My company uses this program to automate long-running tasks and keep our servers running as many at a time as possible without overrunning the system. To do this, my perl script periodically checks the status of its child processes (kicked off with a call to open()) using the waitpid() function. This works like a charm in the Linux world but doesn't perform as expected in the Windows world. The code I think is the problem is the following:
sub check_for_completed_processes { my @completed; my @temp; # print "Checking for completed procs...\n"; while(my $running = pop @running) { if(waitpid($running -> {process_id} ,&WNOHANG)) { $running -> {end_time} = timestamp(); # print "Finished. \n"; push @completed, $running; print "Process completed: " . $running -> {process} . "\n"; print "Started at: " . $running -> {start_time} . "\n"; print "End time: " . $running -> {end_time} . "\n"; } else { push @temp, $running; } } @running = @temp; return @completed; + }
This is being run on Windows Server 2003. Is there a way in the Windows world to do a non-blocking waitpid() call or equivalent?

Replies are listed 'Best First'.
Re: waitpid and Windows
by BrowserUk (Patriarch) on May 31, 2007 at 19:50 UTC
    ... but doesn't perform as expected in the Windows world.

    That's a pretty poor description of the problem. What are you expecting and how does what you are seeing differ from that?

    The non-blocking form of waitpid seems to work fine on my system:

    use POSIX qw[ :sys_wait_h ];; $pid = open O, '| perl.exe -e"sleep 10; exit(123)"' or die $!;; print waitpid( $pid, &WNOHANG ) while sleep 1;; 0 0 0 0 0 0 0 4360 -1 -1 -1 Terminating on signal SIGINT(2)

    So how is it "not performing as expected" for you?

    Also, showing the open being used woudl be a good idea as that can influence things.


    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.
      It seems to hang on the waitpid() call instead of returning immediately and moving on to the next loop step.
        It seems to hang on the waitpid()

        It's not hanging - you're just not seeing the output.

        To see the output you'll need to run BrowserUK's code as perl -l script.pl. Alternatively, you could change the third line of the code to:
        print waitpid( $pid, &WNOHANG ), "\n" while sleep 1;;
        Cheers,
        Rob

        Did you try running the snippet I posted above?

        Here it is as a one-liner in case you couldn't see how to run it. (You'll need to enter it as a single line.)

        C:\test>perl -MPOSIX=:sys_wait_h -le"$pid=open O, q[perl -esleep(10)|];print waitpid $pid,&WNOHANG whil +e sleep 1" 0 0 0 0 0 0 0 0 0 3204 -1 -1 Terminating on signal SIGINT(2)

        You'll need ^C to terminate it. What results did you see?

        If you can eliminate your installation and platform as a source of the problem, then you might look more closely at your implementation. Maybe post a bit more code? Including the open as I requested?


        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.
Re: waitpid and Windows ($])
by tye (Sage) on Jun 01, 2007 at 00:32 UTC

    Perhaps you are just using too old of a version of Perl, one from before async waitpid was supported on Win32.

    - tye        

      I'm using ActiveState perl v.5.8.8