sobrique: (Default)
[personal profile] sobrique
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:

#!/usr/bin/perl

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
This account has disabled anonymous posting.
If you don't have an account you can create one now.
HTML doesn't work in the subject.
More info about formatting

Profile

sobrique: (Default)
sobrique

December 2015

S M T W T F S
  12345
6789101112
13141516171819
20212223242526
2728 293031  

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Jul. 20th, 2025 11:59 pm
Powered by Dreamwidth Studios