Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^3: Need help with Perl multi threading

by BrowserUk (Pope)
on Jan 25, 2012 at 21:20 UTC ( #949970=note: print w/ replies, xml ) Need Help??


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

Sorry to be rude, but you really don't seem to have a clue what you are doing.

This is a completely redundant use of threading:

push (@threads, threads->new(\&Process_Output, $ssh, $proc )); while ( $thread = shift @threads ) { @output=$thread->join(); }

You create a thread and push its handle onto @threads. You then immediately shift that handle off of the array and join it.

  1. There is no point in using an array here, because it will only ever contain at most one handle.
  2. Which makes using the while loop redundant because it will only ever iterate once.

    It is exactly equivalent to:

    $thread = threads->new(\&Process_Output, $ssh, $proc ); if( $thread ) { @output = $thread->join(); }

    Which is equivalent to :

    $thread = threads->new(\&Process_Output, $ssh, $proc ); @output = $thread->join();

    Which is exactly equivalent to this:

    @output = threads->new(\&Process_Output, $ssh, $proc )->join();

    Which is exactly equivalent to:

    @output = Process_Output( $ssh, $proc );

    Except that the latter will work. (Of course, it won't speed things up. But neither will what you have now.) But it will work.

The reason your code is crashing is because you are creating an instance of Net::SSH::Expect in one thread, and trying to use it in another.

Why? Why not create the object in the thread where you are going to use it?


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

The start of some sanity?


Comment on Re^3: Need help with Perl multi threading
Select or Download Code
Re^4: Need help with Perl multi threading
by ashok.g (Beadle) on Jan 25, 2012 at 21:41 UTC
    Thanks for your explanation!

    So, you mean to say keep the thread that will do all like creating a SSH reference, calling function and what not for each server....?

    This is exactly what I started with but I thought thread is having some problems with SSH (a dumb guess) and ignored that.

    I will try that and will post you my findings.

    Thanks,
    Ashok.
      So, you mean to say keep the thread that will do all like creating a SSH reference, calling function and what not for each server....?

      No. I meant rather than create the Net::SSH::Expect object in the loop and then pass it into the thread, pass the information required to create the session into the thread create the session there.

      However, whether that would actually work is predicated upon the module being thread safe and capable of handling concurrent sessions to different servers, which I strongly suspect is not the case, though I do not know for sure and cannot test it.

      The bottom line is I do not think that you will be able to use threading and with Net::SSH::Expect. I would suggest that you contact the author and ask him if the module is thread-safe.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

      The start of some sanity?

        I tried even that but still getting the segmentation fault.... all these trial are not working with Net::SSH::Expect and threads..... :-|
Re^4: Need help with Perl multi threading
by ashok.g (Beadle) on Jan 25, 2012 at 21:52 UTC
    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....

      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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (10)
As of 2014-07-31 08:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (246 votes), past polls