in reply to Re^3: Splitting one large timeout into few smaller ones..
in thread Splitting one large timeout into few smaller ones..

Please provide sample code/pseudocode, because I don't quite follow. Here's how it looks right now:
Event->idle(min=>$mint,max=>$maxt, cb=> [$sth,"method"], max_cb_tm=>$timeout,);
where $timeout is my large timeout value, roughly every $mint seconds $sth->method() is called, if it takes longer then $timeout it is killed off.

I don't know where am I supposed to fork.

Replies are listed 'Best First'.
Re^5: Splitting one large timeout into few smaller ones..
by Roy Johnson (Monsignor) on Apr 07, 2005 at 15:38 UTC
    You would fork inside your
    foreach $task (@tasks) { $task->do_one_task() };
    It might go like this (see fork for more error checking):
    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; } }
    or, probably better:
    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; }

    Caution: Contents may have been coded under pressure.