in reply to Re^9: Help needed with regard to arrays
in thread Help needed with regard to arrays

#!/opt/perl-5.8.8/bin/perl -w # # use strict; use Getopt::Long; use threads; use threads::shared; use Thread::Queue; require Data::Dumper; ######## Do not modify if you do not know what you are doing!!! can de +grade performance!! ######## my $max_thread_count = 10; ###################################################################### +############################ my $version = "0.1"; my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst); ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time +); sub timestamp { my $time; $time = sprintf("%04d-%02d-%02d %02d:%02d:%02d",1900 + $year, $mon ++1, $mday, $hour, $min, $sec); return($time); } my $date = $mday . $mon+1 . 1900+$year; my $promoName; my $storeList; my $workFile; my $logFile; my $ocr; my $userid; my $store; my @stores; my $thread; my $thread_list; my $tid; GetOptions( "promoname=s" => \$promoName, # promo name fo +r logging purposes "sl=s" => \$storeList, # Store List "wf=s" => \$workFile, # Work file "ocr=s" => \$ocr, # ocr "userid=s" => \$userid, # userid ); if (!$promoName || !$storeList || !$workFile || !$userid) { printUsage(); exit 1 } $logFile = "$promoName.$date.$userid.log"; sub writeLog { open(LOGFILE, ">>$logFile"); print LOGFILE &timestamp() . " $_[0]\n"; close(LOGFILE); } # usage instructions sub printUsage { print "USAGE: $0 --promoname=promo name (for logging) - REQUIRED --sl=storelist files (can be seperated by commas) - REQUIRED --wf=work file - REQUIRED --ocr=ocr #### --userid=userid - REQUIRED \n"; } # read store list(s) my $buffer; my $num_lines; sub readSL { my @list = split(/,/, $storeList); foreach my $storefile (@list) { print "opening $storefile\n"; open(STORELIST, "<$storefile"); @stores = <STORELIST>; chomp @stores; @stores = sort @stores; } } writeLog("Starting for $promoName initiated by $userid"); readSL(); my $jobs = Thread::Queue->new(@stores); # read work file sub processWF { my $val = shift; open(WORKFILE, "<$workFile"); my @lines = <WORKFILE>; #@lines = sort @lines; #chop @lines; while (defined (my $item = $jobs->dequeue)) { foreach my $line (@lines) { $line =~ s/storeplaceholder/$item/; print "running: $line"; system($line); } } close(WORKFILE); } $jobs->enqueue(undef) for 1..$max_thread_count; my @workers = map { threads->create( \processWF ) } 1..$max_thread_cou +nt; 0;
Appreciate your help

Replies are listed 'Best First'.
Re^11: Help needed with regard to arrays
by Corion (Patriarch) on Nov 21, 2008 at 08:25 UTC

    Your program will exit early, as you never wait for the threads to end. I've ripped out all the logfile creation, the batch file reading and usage message, and the following program works just like I expect it to, "processing" 10 stores in parallel:

    #!/opt/perl-5.8.8/bin/perl -w # # use strict; use Getopt::Long; use threads; use threads::shared; use Thread::Queue; require Data::Dumper; ######## Do not modify if you do not know what you are doing!!! can de +grade performance!! ######## my $max_thread_count = 10; ###################################################################### +############################ my $version = "0.1_01"; my @stores = sort map { sprintf 'store%03d', $_ } 0..$max_thread_count +*2; my $thread; my $thread_list; my $tid; sub writeLog { print "$_[0]\n"; } my $jobs = Thread::Queue->new(@stores); # read work file sub processWF { my @lines = ( 'perl -wle "print q(storeplaceholder stage 1);sleep(rand(10));pr +int q(storeplaceholder stage 1 done)"', 'perl -wle "print q(storeplaceholder stage 2);sleep(rand(10));pr +int q(storeplaceholder stage 2 done)"', ); while (defined (my $item = $jobs->dequeue)) { writeLog("Launching $item"); foreach my $line (@lines) { $line =~ s/storeplaceholder/$item/g; #print "running: $line\n"; system($line) == 0 or warn "Couldn't launch [$line]: $!/$?"; } } } $jobs->enqueue(undef) for 1..$max_thread_count; my @workers = map { threads->create( \&processWF ) } 1..$max_thread_co +unt; $_->join() for @workers;

    Consider reducing your programs to the mininmum needed code to reproduce the program while still retaining a full, ready-to-run program. This makes it much easier for us to reproduce the problem and help you. Also, in that process, I usually find the error myself, without posting here.

      I appreciate your expert help
      Hi There seems to be a bug with this code....I am trying to figure out whats going on but could use your help.... The rest of the code is the same as you posted but I added a print statement to show whats going on....
      sub processWF { my @lines = ( 'perl -wle "print q(storeplaceholder stage 1);sleep(rand(10));pr +int q(storeplaceholder stage 1 done)" ', 'perl -wle "print q(storeplaceholder stage 2);sleep(rand(10));pr +int q(storeplaceholder stage 2 done)" ', ); while (defined (my $item = $jobs->dequeue)) { writeLog("Launching $item"); foreach my $line (@lines) { print "replacing $line with $item\n"; $line =~ s/storeplaceholder/$item/g; #print "running: $line\n"; system($line) == 0 or warn "Couldn't launch $line: $!"; } } }

        This bug is present in Re^8: Help needed with regard to arrays as well, so it just matches what you originally did. But until you tell me what the bug is, how it manifests itself, I won't tell you. Debugging is something that you'll have to learn if you ever want to become a successful programmer. Just saying "There seems to be a bug" is not helpful. As a hint, look at the @lines array during execution.

        Update: I notice you reposted this same question, with just as little explanation at Problem with threading code. I would have appreciated a notice from you, because I wouldn't have spent time on your problem then.