Greetings ye Monks.

I have a loop that sleeps at the end and I want to make the thing bail out if the whole loop takes too long. The obvious solution:

alarm 10 while ( ! $done ) { $done = is_it_done_yet(); sleep 1; }

... is bad because perlfunc says:

It is usually a mistake to intermix "alarm" and "sleep" calls. ("sleep" may be internally implemented in your system with "alarm")

So what's the "right" way to do this?

Do I want to fork off a watcher process? Is there a simpler way? If there isn't a simpler way, is there a good CPAN module that handles this?

For background: I'm writing an apache log rotater. I want to compress the files once apache has stopped writing to them, which is what is_it_done_yet() is analogous to. My script checks to see if it can start compressing by using lsof (Unix::Lsof) and compresses them using gzip if it can. If lsof or gzip wander off and never return, I want to have some way to abort the whole thing.

Thanks!
--Pileofrogs


In reply to Alternatives to Mixing Alarm and Sleep by pileofrogs

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.