0

I have used the following pattern of code in perl on a Unix system, but it crashes on Windows. How can I achieve the same thing using either forking or threads on Windows using perl?

use Parallel::ForkManager;

my $pm = Parallel::ForkManager->new($MAX_PROCESSES);

DATA_LOOP:
foreach my $data (@all_data) {
    # Forks and returns the pid for the child:
    my $pid = $pm->start and next DATA_LOOP;

    # ... do some work with $data in the child process ...

    $pm->finish; # Terminates the child process
}
9
  • Thread::Queue Commented Jan 4, 2017 at 23:56
  • What would be the equivalent of the above code using Thread::Queue? Commented Jan 5, 2017 at 0:00
  • @choroba It didn't work either. Crashes as well. Commented Jan 5, 2017 at 0:30
  • Forks::Super by @mob should work on Windows. Also note its tips for windows. Try this post Commented Jan 5, 2017 at 1:45
  • 4
    sounds like your perl installation has some problems. tell more about the crashing Commented Jan 5, 2017 at 3:27

1 Answer 1

1

Here is one example using fork:

#!/usr/bin/perl -w
use strict;


foreach my $data (@all_data) {
    my $pid;
    next if $pid = fork;    # Parent goes to next server.
    die "fork failed: $!" unless defined $pid;

    # From here on, we're in the child.  Do whatever the
    # child has to do...  The server we want to deal
    # with is in $data.

    exit;  # Ends the child process.
}

# The following waits until all child processes have
# finished, before allowing the parent to die.

1 while (wait() != -1);

print "All done!\n";
Sign up to request clarification or add additional context in comments.

1 Comment

That's not quite the same because it spawns scalar(@all_data) processes all at once. The OP's example spawns no more than $MAX_PROCESSES processes at a time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.