in reply to Re^12: Thread Design help
in thread Thread Design help
Here's a very basic skeleton that will serve as a basis for you to tweak to do the job. It uses a prioritising subclass of Thread::Queue to 'schedule' the repeat jobs:
#! perl -slw use strict; use threads; use Thread::Queue; use LWP::Simple; { package T::Q::O; use Data::Dump qw[ pp ]; require Thread::Queue; use threads; use threads::shared; our @ISA = 'Thread::Queue'; sub enqueue { local $^W; my $Q = shift; lock @$Q; for( @_ ) { push @$Q, $_; my $n = $#$Q; @{ $Q }[ $n, $n-1 ] = @{ $Q }[ $n-1, $n ], --$n while $n-1 and $Q->[ $n ] < $Q->[ $n - 1 ]; } cond_signal( @$Q ); } sub dump { my $Q = shift; lock @$Q; pp $Q; cond_signal( @$Q ); } } sub dbiFetch { my @args = @_; return getstore $args[ 0 ], 'nul'; } sub sshFetch { my @args = @_; return getstore $args[ 0 ], 'nul'; } sub tcpFetch { my @args = @_; return getstore $args[ 0 ], 'nul'; } sub worker { no strict 'refs'; my $Q = shift; while( my $work = $Q->dequeue ) { my( $time, $interval, $sub, @args ) = split $;, $work; sleep 1 while time() < $time; # printf "now: %.f time:$time int:$interval sub:$sub [@args]", +time(); my $result = $sub->( @args ); # print $result; $Q->enqueue( join $;, $time + $interval, $interval, $sub, @arg +s ); } } our $W //= 10; my $Q = new T::Q::O; my @workers = map async( \&worker, $Q ), 1 .. $W; while( <> ) { chomp; $Q->enqueue( join $;, time(), (map $_*60, 1,2,3 )[ rand 3 ], ( qw[dbiFetch sshFetch tcpFetch ] )[ rand 3 ], $_ ); } 1 while sleep 1;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^14: Thread Design help
by perlCrazy (Monk) on Sep 12, 2010 at 09:03 UTC |