Eyck has asked for the wisdom of the Perl Monks concerning the following question:
foreach $task (@tasks) { $task->do_one_task() };
Taks that could hang include 'do_one_task' and 'foreach'. Each step should finish in under 20 seconds, but because there are up to 100 of steps involved, I need to set unreasonably large timeout value, like 2000 seconds for the whole subroutine.
This is bad because every single step can go into infinite loop, I should be able to detect this in under 30 seconds, and because I'm using aggegate values I won't know about the job hanging until 2000 seconds later.
I would like to split one big timeout value for this whole code block, into few smaller ones, essentially this means that I would like to convert this code into something like this:
Alternatively I would need to use something like coroutines, but I think this would be way beyond my skill, ie:foreach $task (@tasks) { $task->do_one_task(); pong(); # Report back to Event::Stats that I'm # still alive and that it should reset it's timer };
where mysubroutine would recall the place where it last finished, iecall_with_timeout(mysubroutine,$low_timeout);
Which approach is the best (and I'm not sure if this coroutine-like solution is at all possible with perl)?$self->MagicallyRecallState(); # and jump to the correct #codeblock foreach $sth (@sth) { $self->SaveState() && return; $sth->method(); };
update
for (0..int(rand(1000))) { sleep(int(rand(10)));# this takes at most 10s };
|
|---|