Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re: Threaded Application Sequencing/Rendezvous

by roboticus (Chancellor)
on Jul 11, 2012 at 00:33 UTC ( #980987=note: print w/replies, xml ) Need Help??

in reply to Threaded Application Sequencing/Rendezvous


The last time I did a system like that it worked pretty much like this:

#!/usr/bin/perl use strict; use warnings; my %Jobs = ( Action1 => { status=>'READY', }, + Action1a=> { status=>'READY', prereqs => [ qw(Action1) ], }, Action2 => { status=>'READY', }, Action2a=> { status=>'READY', prereqs => [ qw(Action2) ], }, Action3 => { status=>'READY', prereqs => [ qw(Action1a Action2a) ] +, }, Action4 => { status=>'READY', prereqs => [ qw(Action3) ], }, ); my @task_list = sort keys %Jobs; my $cnt=0; while (@task_list) { ++$cnt; print "\nTIME: $cnt\n"; for (sort keys %Jobs) { printf "%-16.16s %-10.10s %u\n", $_, $Jobs{$_}{status}, is_ready($_); } # Clean out the items that are already done @task_list = grep { $Jobs{$_}{status} ne 'DONE' } @task_list; # Each time we wake up, get list of tasks ready to go and fire # them off my @ready = grep { is_ready($_) } @task_list; $Jobs{$_}{status}="RUNNING" for @ready; # Fake code to pretend to monitor tasks & update job list for (@task_list) { $Jobs{$_}{status}="DONE" if $Jobs{$_}{status} eq "RUNNING" and rand() < 0.25; } sleep 1; } sub is_ready { my $task=shift; return 0 if $Jobs{$task}{status} ne 'READY'; return 1 if ! exists $Jobs{$task}{prereqs}; for (@{$Jobs{$task}{prereqs}}) { return 0 if $Jobs{$_}{status} ne 'DONE'; } return 1; }

Basically, each task knows what its prerequisite tasks are, and will fire off only when all its prerequisites are satisfied. The original also handled error cases by simply restarting the failed jobs after a delay. (On that system, most failures were due to missing files, so re-running was sufficient to fix most problems.) Also, I used a database as the scratchpad, so that the system could recover properly if the system went down and then recovered.

I'd post the code, but it was C# rather than perl.


When your only tool is a hammer, all problems look like your thumb.

Replies are listed 'Best First'.
Re^2: Threaded Application Sequencing/Rendezvous
by learnedbyerror (Beadle) on Jul 11, 2012 at 12:15 UTC

    I like the approach regarding the autonomy of the threads. I scratched out something similar while thinking about this yesterday; however, I felt like I was making the solution more complicated than the problem.

    I'm going to get a cup of coffee and think through you model.

    Thanks, lbe

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://980987]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (2)
As of 2017-02-20 23:55 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (304 votes). Check out past polls.