Hello Monks. I've been working on a multithreaded script and have trouble figuring out loggin/printing portion of it.

Each of the worker threads is essentially independent and can run perl code as well as do system() calls. I was originally using Capture::Tiny module and it worked fine enough except when system calls are involved. Whenever system calls come into play, all output gets jumbled up and mixed up.

Can this logging approach be done? If not is there another way? If this cannot be done with threads, can this kind of parallel processing/logging be done somehow differently?

Main script

#!/usr/local/bin/perl use strict; use warnings; use threads; use Capture::Tiny qw/capture_merged/; use IO::File; use IPC::Run qw/run/; use autodie qw/open close/; use FindBin qw/$RealBin/; my $thread1 = threads->create ( \&worker, 1, 'sub1.log' ); my $thread2 = threads->create ( \&worker, 2, 'sub2.log' ); my $need_to_continue = 1; while ($need_to_continue) { print "MAIN THREAD IS PRINTING\n"; if ( $thread1->is_running() || $thread2->is_running() ) { sleep 3; } else { $need_to_continue = 0; } } $thread1->join(); $thread2->join(); exit 0; ################ sub worker { my $num = shift; my $log_file = shift; open ( my $LFH, '>', $log_file ); capture_merged { inner_worker ( $num ); } stdout=>$LFH,stderr=>$LFH; close $LFH; }; sub inner_worker { my $num = shift; print STDOUT "INFO> Some info from sub $num\n"; print STDERR "WARN> Some warn from sub $num\n"; system ("$RealBin/test_err.pl"); print STDERR "INFO> Some info from sub $num\n"; print STDERR "WARN> Some warn from sub $num\n"; };

test_err.pl (simulating random program) called using system

#!/usr/local/bin/perl print "non-error\n"; warn "-E- test error"; warn "-E- test error"; warn "-E- test error"; print "non-error\n"; print "non-error\n"; print "non-error\n"; sleep(1); print "non-error\n"; print "non-error\n"; print "non-error\n"; warn "-E- test error"; warn "-E- test error"; warn "-E- test error"; warn "-E- test error"; warn "-E- test error"; warn "-E- test error"; warn "-E- test error"; warn "-E- test error"; warn "-E- test error"; warn "-E- test error"; warn "-E- test error"; warn "-E- test error"; warn "-E- test error"; warn "-E- test error"; warn "-E- test error"; warn "-E- test error"; warn "-E- test error"; warn "-E- test error"; warn "-E- test error"; warn "-E- test error"; warn "-E- test error"; sleep(1); warn "-E- test error"; warn "-E- test error"; warn "-E- test error"; warn "-E- test error"; print "non-error\n"; print "non-error\n"; print "non-error\n"; print "non-error\n"; print "non-error\n"; print "non-error\n"; print "non-error\n"; sleep(1); print "non-error\n"; print "non-error\n"; print "non-error\n"; warn "-E- test error"; warn "-E- test error"; warn "-E- test error"; print "non-error\n"; print "non-error\n"; print "non-error\n"; print "non-error\n"; print "non-error\n"; print "non-error\n"; sleep(1); print "non-error\n"; sleep(1);

In reply to capturing output of system call inside a thread by that_guy

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.