I am new to writing multi-threaded Perl applications, but I am not a total n00b to perl... well maybe just a kinda n00b ;)

In the past when I wanted to implement a timeout event, I used alarms. But I read in the docs that there can only be one alarm running, so that will not do for my multi-threaded program with several worker threads all needing their own alarm concurrently.

The tasks my worker threads are performing is running bash test scripts that they receive from an action queue, and reporting the status of tests to a finished queue. The scripts run time ranges from minutes to several hours, but the stdout should change for each script relatively often. If the stdout does not change for a given action for a significant period of time, then the bash script has frozen, so i need to record the fail and run the next script.

Here is the general idea of the approach I would take in a single process program.

eval { open (HANDLE, " $cmdstring |"); alarm $timeout; while (HANDLE) { #process and record info from command's stdout alarm $timeout; #update alarm } alarm 0; }; if ($@) { #timeout occurred #record the fail } else { # do normal stuff }

I have thought of writing system times to a shared variable for each worker, and then run another thread to work as a watchdog for the other processes, and kill threads as they hang, but this would be very messy and I would have to create new threads to fill the place, which could result in some memory nightmares and other issues...


In reply to Thread Safe alarms? by Pocadotty

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.