Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

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]
[perldigious]: I watched some video on YT awhile back with auto-subtitle on and the speaker had a very thick cockney sort of English accent... hillarity ensued in the subtitles.
[Discipulus]: IHAA=I hate acronyms anyway
[perldigious]: If you want a linguistic adventure...
[perldigious]: No offense to any Scotsman, I love Scots. Well actually, I love Scotch, but I'm sure the people are great too. :-P
[Discipulus]: perldigious i understand i word on ten, to be optimistic..
LanX will try to give his next LPW talk with a Cogney intonation
[Discipulus]: if you love Scotch in scotland your safety will be.. glengranted

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2017-06-23 16:47 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (552 votes). Check out past polls.