Log in

No account? Create an account
entries friends calendar profile Previous Previous Next Next
Perl - basic threading example - Ed's journal
Perl - basic threading example
This is perhaps in the wrong order, but to follow on from a couple of rambles lately - threading and perl.
How do you basically do this?
Here is an example:


use strict;
use warnings;
use threads;

my $thread_count = 100; 

sub thread_subroutine
  #very simplistic thread - sleeps for a random time interval,
  #based on a passed in parameter. 
  my ( $sleep_time ) = @_;
  my $delay = rand ( $sleep_time ); 
  sleep ( $delay );
  return $delay;

for ( $count = 0; $count < $thread_count; $count++)
  my $thread = threads -> create ( \&thread_subroutine, 60  );

#this evaluates as 'true' as long as the list is not
#an empty list. (e.g. there are still running threads)
while ( threads -> list() )
  #run through the list, and check for running state
  #more as an example than because it's useful, because we 
  #only do the 'join' later. 
  foreach my $thread ( threads -> list() )
    if $thread -> is_joinable() { print "Thread ID: ",$thread -> tid(), " is ready to join\n"; } 
    if $thread -> is_running() { print "Thread ID: ", $thread -> tid(), " is still running\n"; }

  #more normal construct for reaping threads as they complete
  foreach my $thread ( threads -> list ( threads::joinable ) )
    my $result = $thread -> join(); 
    print "Thread ID: ", $thread -> tid(), " is complete - returned a result of ", $result, "\n"

OK, that's pretty simplistic I know - but the major way I've found threads useful is for what amount to embarrassingly parallel problems, like 'connect to 200 servers, and run the same commands on each of them'. You could quite easily replace that rather trivial 'sleep' subroutine, with one that does an ssh to a host, to run a command and capture the output. (And maybe process the results, before returning them)

Also note: Perl has had threads for a while, but the module doesn't necessarily contain all the functions you need - latest version as of 2012-07-23 is 1.86 which is available from CPAN. (http://search.cpan.org/~jdhedden/threads-1.86/lib/threads.pm)

More detail: http://perldoc.perl.org/threads.html

Tags: ,

1 comment or Leave a comment
warmage From: warmage Date: July 27th, 2012 05:20 am (UTC) (Link)
and so from here to make a functional script, you're throttling the number of live threads, error trapping, logging/tailing the output, etc... Yup. Cheers :)
1 comment or Leave a comment