Here's the situation - I'm using Event, and am using it's timeout feature (Event::Stats). However, the procedure that's beeing watched contains variable number of steps, logically this goes like this
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:

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 };
Alternatively I would need to use something like coroutines, but I think this would be way beyond my skill, ie:
call_with_timeout(mysubroutine,$low_timeout);
where mysubroutine would recall the place where it last finished, ie
$self->MagicallyRecallState(); # and jump to the correct #codeblock foreach $sth (@sth) { $self->SaveState() && return; $sth->method(); };
Which approach is the best (and I'm not sure if this coroutine-like solution is at all possible with perl)?

update

for (0..int(rand(1000))) { sleep(int(rand(10)));# this takes at most 10s };

In reply to Splitting one large timeout into few smaller ones.. by Eyck

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.