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 8081331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:41:56+00:00 2026-06-05T16:41:56+00:00

Is it possible to implement some kind of timeout (time limit) for fork using

  • 0

Is it possible to implement some kind of timeout (time limit) for fork using Parallel::ForkManager ?

Basic Parallel::ForkManager script looks like this

use Parallel::ForkManager;
my $pm = Parallel::ForkManager->new( 10 );
for ( 1 .. 1000 ) {
    $pm->start and next;
    # some job for fork
    $pm->finish;
}
$pm->wait_all_children();

I would like to limit time for “# some job for fork”. For example, if its not finished in 90 secs. then it (fork) should be killed/terminated.
I thought about using this but I have to say, that I dont know how to use it with Parallel::ForkManager.

EDIT

Thanks hobbs and ikegami. Both your suggestions worked….. but only in this basic example, not in my actual script :(.
screenshot
These forks will be there forever and – to be honest – I dont know why. I use this script for couple of months. Didnt change anything (although many things depends on outside variables).
Every fork has to download a page from a website, parse it and save results to a file. It should not take more than 30 secs per fork. Timeout is set to 180 secs. Those hanging forks are totally random so its very hard to trace the problem. Thats why I came up with a temporary, simple solution – timeout & kill.

What could possibly disable (interrupt) your methods of timeout in my code ? I dont have any other alarm() anywhere in my code.

EDIT 2

One of the forks, was hanging for 1h38m and returned “timeout PID” – which is what I type in die() for alarm(). So the timeout works… but its late about 1h36,5m ;). Do you have any ideas?

  • 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-05T16:41:58+00:00Added an answer on June 5, 2026 at 4:41 pm

    Update

    Sorry to update after the close, but I’d be remiss if I didn’t point out that Parallel::ForkManager also supports a run_on_start callback. This can be used to install a “child registration” function that takes care of the time()-stamping of PIDs for you.

    E.g.,

    $pm->run_on_start(sub { my $pid = shift; $workers{$pid} = time(); });
    

    The upshot is that, in conjunction with run_on_wait as described below, the main loop of a P::FM doesn’t have to do anything special. That is, it can remain a simple $pm->start and next, and the callbacks will take care of everything else.

    Original Answer

    Parallel::ForkManager’s run_on_wait handler, and a bit of bookkeeping, can force hanging and ALRM-proof children to terminate.

    The callback registered by that function can be run, periodically, while the $pm awaits child termination.

    use strict; use warnings;
    use Parallel::ForkManager;
    
    use constant PATIENCE => 90; # seconds
    
    our %workers;
    
    sub dismiss_hung_workers {
      while (my ($pid, $started_at) = each %workers) {
        next unless time() - $started_at > PATIENCE;
        kill TERM => $pid;
        delete $workers{$pid};
      }
    }
    
    ...
    
    sub main {
      my $pm = Parallel::ForkManager->new(10);
      $pm->run_on_wait(\&dismiss_hung_workers, 1);  # 1 second between callback invocations
    
      for (1 .. 1000) {
        if (my $pid = $pm->start) {
          $workers{$pid} = time();
          next;
        }
        # Here we are child.  Do some work.
        # (Maybe install a $SIG{TERM} handler for graceful shutdown!)
        ...
        $pm->finish;
      }
    
      $pm->wait_all_children;
    
    }
    

    (As others suggest, it’s better to have the children regulate themselves via alarm(), but that appears intermittently unworkable for you. You could also resort to wasteful, gross hacks like having each child itself fork() or exec('bash', '-c', 'sleep 90; kill -TERM $PPID').)

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

Sidebar

Related Questions

Is it possible to implement basic arithmetic (at least addition) in C# generics, like
Is it possible to implement some kind of decorator component in wicket ? Specially
Caching in ASP.NET looks like it uses some kind of associative array: // Insert
Is it possible to employ some kind of prototypal inheritance in PHP like it
Possible Duplicate: Why would you ever implement finalize()? I saw some java files with
Is it possible to implement a timeout in an inputstream.read() function from a BluetoothSocket
Is is possible to implement smooth scrolling using both a UIScrollView and placing a
It's possible to implement INotifyCollectionChanged or other interface like IObservable to enable to bind
Currently I'm trying to implement some kind of object stream, similar to the structure
I'd like to create a game with some kind of a Roulette Wheel with

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.