Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Redirecting output in Windows cmd prevents second thread from doing anything

by dasgar (Priest)
on Jan 15, 2016 at 19:23 UTC ( [id://1152889]=note: print w/replies, xml ) Need Help??


in reply to Redirecting output in Windows cmd prevents second thread from doing anything

First, just wanted to point out that the documentation for the Thread module indicates that it is deprecated and recommends users to use the threads module instead (see DEPRECATED section). That has nothing to do with your question, but might help you to avoid other issues down the road.

For creating a log of everything from all of the threads, you could create a variable in each thread that would contain the log contents of that thread and have the thread return that value. Then the main code section can print that out to a file afterwards.

Here's an untested modification of your code that will does what I just described.

#create the array with the ip:OID use warnings; use strict; use Thread qw(async); use threads::shared; share (my @array); @array = qw/2.1.1.1:1.3.1.8.1 2.1.1.1:1.3.1.8.2 2.1.1.1:1.3.1.8.3 2.1. +1.1:1.3.1.8.4 2.1.1.1:1.3.1.8.5 2.1.1.1:1.3.1.8.6 2.1.1.1:1.3.1.8.7.. +..sequence continues upto....2.1.1.1:1.3.1.8.199 2.1.1.1:1.3.1.8.200/ +; my $thr1 = async { my $log = ""; while($#array > 0) { print "Thread 1: size of the array is $#array\n"; my @my_oids; { lock (@array); # Block until we get access to $a for(my $i=0;$i<10;$i++) { push (@my_oids, (pop @array)); } } foreach(@my_oids) { print "Thread 1: Doing SNMP GET for $_\n" if (defined($_)) +; $log .= "Thread 1: Doing SNMP GET for $_\n"; } } return $log; }; my $thr2 = async { my $log = ""; while($#array > 0) { print "Thread 2: Size of the array is $#array\n"; my @my_oids; { lock (@array); # Block until we get access to $a for(my $i=0;$i<10;$i++) { push (@my_oids, (pop @array)); } } foreach(@my_oids) { print "Thread 2: Doing SNMP GET for $_\n" if (defined($_)) +; $log .= "Thread 2: Doing SNMP GET for $_\n"; } } return $log; }; my $log1 = $thr1->join; my $log2 = $thr2->join; my $file = 'threadinfo.txt' open(my $output,">",$file) or die "Unable to open file '$file': $!"; print $output $log1; print $output $log2; close($output);

Of course, one drawback on that would be that if a thread were to die prematurely, you would lose all logging for that thread. Since you are already using a shared variable for the threads to read from, you could also use one or more shared variables that the threads could use for logging. And again, write that out to file after joining all of the threads.

And if you are needing/wanting the logging to be intermixed, just add a timestamp for each log entry from each thread. Then you could sort the entries from all of the logs afterwards to create a single chronologically ordered log from the contents of all of the thread logs.

  • Comment on Re: Redirecting output in Windows cmd prevents second thread from doing anything
  • Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1152889]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2024-04-23 09:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found