Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

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 ''; my @servers = ('','',''); 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 Unable to SSH to Unable to SSH to Unable to SSH to
Now again back to the stage where I have started my script....

Replies are listed 'Best First'.
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.


        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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://949981]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2018-02-25 02:52 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (312 votes). Check out past polls.