Hi monks, I had a question about implementing an output queue from multiple threads, using Thread::Queue. It's "working" without warnings or errors, but the memory usage gets way out of control, and it's also a lot slower than using locks and shared variables (which I assume it cause I screwed something up). I understand the memory usage would go up if the output queue was getting more than it could handle. In any case I can't seem to find many examples so any advice would be appreciated.

The reason I wanted to use an output queue is that the worker queue is processing very large files on different computers (so it made sense to use threads). The output needs to be serialized. Previously I used a lock as I said. I heard that using an output queue would be better design. So I want to pass the output from the worker threads to the output queue and print the data there.

sub runSearch { my $outputQueue = Thread::Queue->new(); my $outputThread = threads->create(\&outputSub,$outputQueue); my $workerQueue = Thread::Queue->new(); my @workerThreads = map threads->create(\&workerSub,$workerQueue,$ou +tputQueue), 1..$numThreads; $workerQueue->engueue($_) foreach @computer; $workerQueue->enqueue(undef) for 1..$numThreads; $outputQueue->enqueue(undef); $_->join for @workerThreads; $outputThread->join; } sub workerSub { my ($workerQueue,$outputQueue) = @_; while (my $computer = $workerQueue->dequeue()) { ## SSH command retrieving grep results and processing $outputQueue->enqueue($results); } } sub outputSub { my $outputQueue = $_; while (my $packet = $outputQueue->dequeue()) { print $results; } }

In reply to Output Queue from Multiple Threads by bigbot

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.