Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Interrupt multi-process program while using MCE::Shared hash: END block code does not (all) run

by Anonymous Monk
on Apr 07, 2017 at 13:28 UTC ( [id://1187396]=note: print w/replies, xml ) Need Help??


in reply to Interrupt multi-process program while using MCE::Shared hash: END block code does not (all) run

# $SIG{'INT'} = sub { kill 'TERM', -$$ }; # this works already $SIG{'TERM'} = sub { MCE->exit(0) }; # Notifies the parent

This is another possibility.

$SIG{'TERM'} = sub { if (MCE->wid > 0) { # worker MCE->exit(0); } else { # parent MCE::Signal::stop_and_exit('TERM'); } };
  • Comment on Re: Interrupt multi-process program while using MCE::Shared hash: END block code does not (all) run
  • Select or Download Code

Replies are listed 'Best First'.
Re^2: Interrupt multi-process program while using MCE::Shared hash: END block code does not (all) run
by 1nickt (Canon) on Apr 07, 2017 at 13:48 UTC

    Thank you for the reply ( marioroy? )!

    In the first example, it was necessary to move the SIG handlers below the mce_loop statement, else it gave error:

    MCE::exit: method is not allowed by the manager process at mce.pl line + 19.

    After moving the SIG handlers, it gives no error but does not appear to execute any of the code in END:

    PID 18566 Forked child with 18568 Forked child with 18569 Forked child with 18571 Forked child with 18572 Forked child with 18570 Forked child with 18573 ^C ## mce.pl: caught signal (INT), exiting Killed
    The second example you provided gives the same result:
    PID 18874 Forked child with 18877 Forked child with 18876 Forked child with 18879 Forked child with 18878 Forked child with 18880 Forked child with 18881 ^C ## mce3.pl: caught signal (INT), exiting Killed

    Note that the program runs successfully if left to complete:

    PID 19017 Forked child with 19019 Forked child with 19020 Forked child with 19023 Forked child with 19022 Forked child with 19024 Forked child with 19021 Forked child with 19019 Forked child with 19020 Forked child with 19022 Forked child with 19023 19020 1491572765.74874 (Child) in END 19019 1491572765.75111 (Child) in END 19024 1491572765.75325 (Child) in END 19022 1491572765.7533 (Child) in END 19021 1491572765.7552 (Child) in END 19023 1491572765.75711 (Child) in END 19017 1491572765.75939 (Parent) in END Parent is ready to dump Dumping: $VAR1 = { '00 19019' => '1491572745.74377', '01 19019' => '1491572746.74401', '02 19019' => '1491572747.74421', '03 19019' => '1491572748.74441', '04 19019' => '1491572749.74457', '05 19019' => '1491572750.74473', '06 19019' => '1491572751.74493', '07 19019' => '1491572752.7451', '08 19019' => '1491572753.74529', '09 19019' => '1491572754.7455', '10 19020' => '1491572745.7436', '11 19020' => '1491572746.74385', '12 19020' => '1491572747.74405', '13 19020' => '1491572748.74427', ... }

    Thank you again for the help.


    The way forward always starts with a minimal test.
Re^2: Interrupt multi-process program while using MCE::Shared hash: END block code does not (all) run
by Anonymous Monk on Apr 07, 2017 at 13:36 UTC

    $SIG{INT} points to MCE::Signal::stop_and_exit by MCE::Signal. Ditto for $SIG{TERM}. Folks can override that if need be like you've done. In that case, the following is needed.

    $SIG{'TERM'} = sub { if (MCE->wid > 0) { # worker MCE->exit(0); } else { # parent MCE::Signal::stop_and_exit('TERM'); } };
Re^2: Interrupt multi-process program while using MCE::Shared hash: END block code does not (all) run
by Anonymous Monk on Apr 07, 2017 at 13:31 UTC

    MCE->exit is important, before the worker exits. The manager process substracts the count by one and reaps the worker.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1187396]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2024-04-20 10:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found