that_guy has asked for the wisdom of the Perl Monks concerning the following question:
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);
|
|---|