in reply to Re^3: Interrupt multi-process program while using MCE::Shared hash: END block code does not (all) run
in thread Interrupt multi-process program while using MCE::Shared hash: END block code does not (all) run
Also, dumper traverses the hash. A mutex is likely necessary around Dumper.
use strict; use warnings; use feature 'say'; use Data::Dumper; ++$Data::Dumper::Sortkeys; use MCE::Shared; use MCE::Loop; $|++; my $pid = $$; say "PID $pid"; my $mutex = MCE::Mutex->new(); tie my %hash, 'MCE::Shared', (); $SIG{'INT'} = sub { my $signal = shift; say 'Hello from INT'; if ( tied(%hash)->len ) { (MCE->wid == 0) ? say 'Parent is ready to dump' : say 'Worker is ready to dump'; say 'Dumping: ' . Dumper \%hash; %hash = (); } MCE::Signal::stop_and_exit($signal); }; $SIG{'TERM'} = sub { my $signal = shift; say 'Hello from TERM'; if (MCE->wid > 0) { # worker MCE->exit(0); } else { # parent say 'Parent is ready to dump'; say 'Dumping: ' . Dumper \%hash; MCE::Signal::stop_and_exit($signal); } }; MCE::Loop->init( max_workers => 2, chunk_size => 1 ); mce_loop { my ( $mce, $chunk_ref, $chunk_id ) = @_; say sprintf 'Forked worker in slot %s with pid %s for chunk %s', M +CE->wid, MCE->pid, $chunk_id; for ( @{ $chunk_ref } ) { $hash{ sprintf '%.2d %s', $_, $$ } = time; $mutex->synchronize( sub { say "After $_: " . Dumper \%hash; }); sleep 1; } } ( 0 .. 4 ); MCE::Loop->finish; END { say sprintf '%s %s (%s) in END', $$, time, $$ == $pid ? 'Parent' : + 'Child'; if ( MCE->wid == 0 or $$ == $pid ) { say "Parent is ready to dump"; say 'Dumping: ' . Dumper \%hash; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: Interrupt multi-process program while using MCE::Shared hash: END block code does not (all) run
by 1nickt (Canon) on Apr 07, 2017 at 16:06 UTC | |
by Anonymous Monk on Apr 07, 2017 at 16:47 UTC | |
by Anonymous Monk on Apr 07, 2017 at 17:06 UTC | |
by 1nickt (Canon) on Apr 07, 2017 at 18:43 UTC |