Beefy Boxes and Bandwidth Generously Provided by pair Networks kudra
Perl Monk, Perl Meditation
 
PerlMonks  

Re^4: Need help with Perl multi threading

by ashok.g (Beadle)
on Jan 25, 2012 at 21:52 UTC ( #949981=note: print w/ replies, xml ) Need Help??


in reply to Re^3: Need help with Perl multi threading
in thread Need help with Perl multi threading

Now this code:

#!/usr/bin/perl use threads; use strict; use warnings; use Net::SSH::Expect; require 'total.pl'; my @servers = ('192.168.0.18','192.168.0.19','192.168.0.20'); my @process = ('testing','b','c'); my @threads; my $output; my $res; my $final; #@output = threads->new(\&Process_Output, $ssh, $proc )->join(); foreach my $host ( @servers ) { threads->new(\&process, $host )->join(); } sub process() { my $host = shift; my $png = `ping -c1 $host`; if ( $png =~ /64 bytes/ ) { my $user= 'netcool'; my $ssh = &SSH_Reference($host,$user); $ssh->run_ssh(); eval {$res=$ssh->read_all(); }; if ( !$@ && $res !~ /\(yes\/no\)/ && $res !~ m/password/ ) { print "\nLogged in to $host\n"; foreach my $proc ( @process ) { #push (@threads, threads->new(\&Process_Output, $s +sh, $proc )); $output=&Process_Output($ssh, $proc ); #print @output; if ( $output =~ m/$proc/ ) { print "\t$proc is runn +ing\n"; } else { print "\t$proc is not +running\n"; } } $ssh->close(); } else { print "Unable to SSH to $host\n"; } } else { print "\nUnable to ping to Host: $host\n"; } }
produces this output:
perl some.pl Unable to SSH to 192.168.0.18 Unable to SSH to 192.168.0.19 Unable to SSH to 192.168.0.20
Now again back to the stage where I have started my script....


Comment on Re^4: Need help with Perl multi threading
Select or Download Code
Re^5: Need help with Perl multi threading
by chromatic (Archbishop) on Jan 25, 2012 at 23:36 UTC

    With regard to:

    foreach my $host ( @servers ) { threads->new(\&process, $host )->join(); }

    ... creating a new thread and then immediately joining it is a waste of time; join waits for the thread to finish.

    In this case, detach is probably better. Otherwise, keep an array of all threads and after you've created all of them, then join all of them.


    Improve your skills with Modern Perl: the free book.

      This was completely opposite to the comment posted in this node

      Anyways I got my task completed by using Net::OpenSSH.

      Thanks,
      Ashok

        BrowserUK and I said essentially the same thing. If you write your thread code as you did, you're not getting any advantage of threading. All of your threads will run serially.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (11)
As of 2014-04-18 12:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (466 votes), past polls