use strict ; use warnings ; use MCE::Hobo ; use MCE::Shared ; use Data::Dumper ; sub task1 { print "Starting task 1 for $_[0]\n" ; sleep 2 ; print "Finished task 1 for $_[0]\n" ; } sub task2 { print "Starting task 2 for $_[0]\n" ; sleep 4 ; print "Finished task 2 for $_[0]\n" ; } sub task3 { print "Starting task 3 for $_[0]\n" ; sleep 6 ; print "Finished task 3 for $_[0]\n" ; } MCE::Hobo->init( max_workers => 2, # hobo_timeout => 10, # posix_exit => 1, ) ; my $test = MCE::Shared->hash ; # Must call STORE (not set) for deeply sharing to work. $test->STORE( L1_counter1 => 1 ) ; $test->STORE( nested1 => { 'L2_counter1' => 3 } ) ; sub executeTasks { my $in = $_[0] ; foreach( sort $in->keys ) { my $ref = ref( my $val = $in->get( $_ ) ) ; if ( $ref && $val->blessed eq 'MCE::Shared::Hash' ) { executeTasks( $val ) ; } else { if ( $val == 1 ) { mce_async { task1( $_ ) ; $in->incr( $_ ) ; } ; } elsif ( $val == 2 ) { mce_async { task2( $_ ) ; $in->incr( $_ ) ; } ; } elsif ( $val == 3 ) { mce_async { task3( $_ ) ; $in->incr( $_ ) ; } ; } ; } ; } ; } ; # Dump shared hash. print Dumper( $test->export( { unbless => 1 } ) ) ; # Begin processing. executeTasks( $test ) ; # Reap any remaining Hobo workers. MCE::Hobo reaps workers # automatically to not exceed max_workers when given. MCE::Hobo->waitall ; # Dump shared hash. print "\n", Dumper( $test->export( { unbless => 1 } ) ) ;