Yes, hangs when pressing Control-C.

Off topic, the OO interface is so nice. The mutex is not necessary. Btw, it is a bit much to have both the handlers and the END block. Only one is needed. But it still hangs when pressing Ctrl-C. However, sending INT from another terminal works fine. For this post, wanted to remove the TIE interface. Instead, access the shared hash via the OO interface. MCE::Shared can handle dereferencing automatically for setting the value. Thus, giving you both OO and dereferencing when needed.

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 $hash = MCE::Shared->hash(); $SIG{'INT'} = sub { my $signal = shift; $SIG{'INT'} = $SIG{'TERM'} = sub { }; say "Hello from $signal: $$"; if ( $hash->len ) { ( MCE->wid == 0 ) ? say 'Parent is ready to dump' : say 'Worker is ready to dump'; say 'Dumping: ' . Dumper $hash->export; $hash->clear; } MCE::Signal::stop_and_exit($signal); }; $SIG{'TERM'} = sub { my $signal = shift; $SIG{'INT'} = $SIG{'TERM'} = sub { }; say "Hello from $signal: $$"; if (MCE->wid) { # worker MCE->exit(0); } else { # parent say 'Parent is ready to dump'; say 'Dumping: ' . Dumper $hash->export; 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; say "After $_: " . Dumper $hash->export; sleep 3; } } ( 0 .. 12 ); MCE::Loop->finish; say "Parent is ready to dump"; say 'Dumping: ' . Dumper $hash->export;

There is the main process, workers, and also the shared-manager process. Something is stalling somewhere.


In reply to Re^6: Interrupt multi-process program while using MCE::Shared hash: END block code does not (all) run by Anonymous Monk
in thread Interrupt multi-process program while using MCE::Shared hash: END block code does not (all) run by 1nickt

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.