in reply to Re^4: Splitting one large timeout into few smaller ones..
in thread Splitting one large timeout into few smaller ones..
It might go like this (see fork for more error checking):foreach $task (@tasks) { $task->do_one_task() };
or, probably better:foreach $task (@tasks) { use POSIX ':sys_wait_h'; my $child = fork(); if ($child) { for (my $polls = 30; waitpid($child, WNOHANG) == 0; --$polls) +{ if ($polls < 1) { print "Game over...killing $child\n"; kill 'ABRT', $child; # Maybe return some code, or exit here } sleep 1; } } else { $task->do_one_task() exit 0; } }
my $child = fork(); if ($child) { $SIG{ALRM} = sub { print "Game over...killing $child\n"; kill 'ABRT', $child; # Maybe return some code, or exit here }; alarm 10; # Waiting for child or alarm waitpid($child, 0); } else { $task->do_one_task() exit 0; }
|
|---|