RE: Executing commands in the system via threads

[email protected] ("Jerry D. Hedden")
Newsgroups perl.ithreads
Message-ID <20060227121908.fb30e530d17747c2b054d625b8945d88.923d1a98ee.wbe@email.secureserver.net>
darronm (ukmay) wrote:
> I need to call an executable program multiple times and I
> will then need to process the returns to stdout.
> ...
> However as soon as I change the line above in sub1 to be:
>    my @value = `vcover stats $FileName `;
> The script hangs.

Well, one problem I noted is that you're trying to use the old
'Thread' module.  I tried something similar using 'threads',
and it worked fine:

#!/usr/bin/perl

use strict;
use warnings;
$| = 1;

use threads;

MAIN:
{
    my @thr1 = threads->create(\&sub1, "ls -l /c");
    my @thr2 = threads->create(\&sub1, "ls -l /var");
    my @thr3 = threads->create(\&sub1, "ls -l /usr");

    foreach my $thr (threads->list) {
        my @output = $thr->join();
        print("Thread returned:\n", @output, "\n");
    }
}

exit(0);

### Subroutines ###

sub sub1 {
    my $cmd = shift;
    print "Command: $cmd\n";
    my @output = `$cmd`;
    return (@output);
}

# EOF
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.