Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8246037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T22:22:43+00:00 2026-06-07T22:22:43+00:00

I am trying to use Parallel::ForkManager to control some child processes. I would like

  • 0

I am trying to use Parallel::ForkManager to control some child processes. I would like to limit the number of processes running concurrently to 10. In total I need to run 20.

I know I could set the process limit at 10 in the first line at the object declaration, but I am also using the $pm object to run child processes that do something different (the current function is much more memory intensive so needs to be limited).

The code I have currently does not work, the run on finish call is never made, so the remaining 10 children never get forked. I don’t understand why this is the case- I’d have thought the child would still call the finish code on exit,and decrement the count, but the “if” statement seems to stop this. Could someone explain why this is the case?

Thanks for any help!

# Parallel declarations
my $pm = Parallel::ForkManager->new(30);

$pm->run_on_finish(sub {
    my ($pid, $exit_code, $ident, $exit_signal, $core_dump, $data_str_ref) = @_; 
    --$active_jobs;
    })

my $total_jobs = 0;
my $active_jobs = 0;
while( $total_jobs < 20) {
    sleep 300 and next if $active_jobs > 10; 

    my $pid = $pm->start and ++$active_p1_jobs and ++$total_p1_jobs and next;

    my $return = module::function(%args);

    $pm->finish(0, { index => $total_jobs, return => $return }); 
    }

print STDERR "Submitted all jobs, now waiting for children to exit.\n";
$pm->wait_all_children();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T22:22:45+00:00Added an answer on June 7, 2026 at 10:22 pm

    I’m going to call “type 2” the jobs that are limited to 10.

    This is how I’d do it with P::FM:

    use strict;
    use warnings;
    
    use List::Util            qw( shuffle );
    use Parallel::ForkManager qw( );
    use POSIX                 qw( WNOHANG );
    use Time::HiRes           qw( sleep );
    
    use constant MAX_WORKERS       => 30;
    use constant MAX_TYPE2_WORKERS => 10;
    
    sub is_type2_job { $_[0]{type} == 2 }
    
    my @jobs = shuffle(
       ( map { { type => 1, data => $_ } } 0..19 ),
       ( map { { type => 2, data => $_ } } 0..19 ),
    );
    
    my $pm = Parallel::ForkManager->new(MAX_WORKERS);
    
    my $type2_count = 0;
    $pm->run_on_finish(sub {
       my ($pid, $exit_code, $ident, $exit_signal, $core_dump, $job) = @_;
       --$type2_count if is_type2_job($job);
       print "Finished: $pid, $job->{type}, $job->{data}, $job->{result}\n";
    });
    
    my @postponed_jobs;
    while (@postponed_jobs || @jobs) {
       my $job;
       if (@postponed_jobs && $type2_count < MAX_TYPE2_WORKERS) {
          $job = shift(@postponed_jobs);
       }
       elsif (@jobs) {
          $job = shift(@jobs);
          if ($type2_count >= MAX_TYPE2_WORKERS && is_type2_job($job)) {
             push @postponed_jobs, $job;
             redo;
          }
       }
       # elsif (@postponed_jobs) {
       #     # Already max type 2 jobs being processed,
       #     # but there are idle workers.
       #     $job = shift(@postponed_jobs);
       # }
       else {
          local $SIG{CHLD} = sub { };
          select(undef, undef, undef, 0.300);
          $pm->wait_one_child(WNOHANG);
          redo;
       }
    
       ++$type2_count if is_type2_job($job);
    
       my $pid = $pm->start and next;
       $job->{result} = $job->{data} + 100;  # Or whatever.
       $pm->finish(0, $job);
    }
    
    $pm->wait_all_children();
    

    But this is broken. The code that picks the next job should be done in the middle of start (i.e. after it waits for children to finish, but before it forks), not before start. This could cause jobs to be run out of order. This isn’t the first time I’ve wished P::FM has a pre-fork callback. Maybe you could ask the maintainer for one.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to use the new Parallel.ForEach function to loop through a datatable
I am trying to use the parallel task library to kick off a number
I'm trying to learn how to use the Control.Parallel module, but I think I
I'm trying to use the ISHFT() function to bitshift some 32-bit integers in parallel,
I'm trying to use the gsh command on ubuntu to do some embarrassingly parallel
I'm trying to use scala parallel collections to implement some cpu-intensive task, I've wanted
I'm trying to make use of parallel processing with the wordnet package for R
I'm trying to use the Unix command paste, which is like a column-appending form
This is some sample test app i trying to learn Parallel.Foreach Loop functionality static
I'm trying to use the Task-Parallel-Library to offload expensive ADO.NET database access from the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.