Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Issue with Threads while collecting command output

by pavan6754 (Initiate)
on Nov 07, 2011 at 16:39 UTC ( [id://936537]=perlquestion: print w/replies, xml ) Need Help??

pavan6754 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Guys,

Below Program creates 3 Threads and each execute ls command for given input path .But this program hangs,seems to be issue with executing ls command.can you please help me out here.

(Note: i don't want to use here lock,as it will be sequential execution not parallel )

use threads; use threads::shared; my %hash:shared =1; my @array=("c:\\Myscripts","c:\\Temp","c:\\Dropbox"); foreach(@array){ push @threads, threads->create(\&execute_ls, $_); } foreach (@threads) { $_->join(); } while(($key,$val)=each %hash){ print("$key:$val\n"); } sub execute_ls{ my $ip=shift; $hash{$ip}=`ls -1 $ip`; }

Replies are listed 'Best First'.
Re: Issue with Threads while collecting command output
by BrowserUk (Patriarch) on Nov 07, 2011 at 16:51 UTC

    Your script runs fine on my machine.


    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.
Re: Issue with Threads while collecting command output
by kennethk (Abbot) on Nov 07, 2011 at 16:54 UTC
    There does not appear to be anything at issue in your Perl code. What happens when you try to run your specified commands on the command line? What you describe sounds more like an issue with your OS/shell and not with Perl.
Re: Issue with Threads while collecting command output
by zentara (Archbishop) on Nov 08, 2011 at 11:18 UTC
    But this program hangs

    The only thing I can see wrong, which may cause hanging, is the way you wait for the thread joins to occur.

    foreach(@array){ push @threads, threads->create(\&execute_ls, $_); } # when you loop thru @threads to join, join will wait for each thread to finish and becoming joinable foreach (@threads) { $_->join(); } # better would be foreach (@threads) { if ( ( $_ -> is_joinable) { $_->join(); } } alternatively you can detach your threads, instead of joining them, and write your return values to shared variables

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
      The only thing I can see wrong, which may cause hanging, is the way you wait for the thread joins to occur.

      Since he is waiting for all threads to finish, the order in which they are joined is irrelevant.

      And using your method, if the first thread (or the first two or the first three) are not yet ready when he tests to see if they are joinable, they will never be joined because your for loop will end and you never go back to re-try them.

      You would have to code that as:

      while( @threads ) { foreach (@threads) { if ( ( $_ -> is_joinable) { $_->join(); } } }

      But that would be pointless.


      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.
        Just checking to see if you had your coffee today...;-), I'm glad you caught that for-while loop oversight.

        I'm not really a human, but I play one on earth.
        Old Perl Programmer Haiku ................... flash japh

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://936537]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2024-04-19 14:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found