MCE::Signal(3)
π MCE::Signal β bestie edition π
okay so this is MCE::Signal, part of the User Contributed Perl Documentation, and it's literally the page that handles making your temp dir, cleaning your temp dir, and catching signals so nothing goes feral. π
β¨ NAME β¨
MCE::Signal β Temporary directory creation/cleanup and signal handling π¦
πΈ VERSION πΈ
This document describes MCE::Signal version 1.902. no cap. π
π«Ά SYNOPSIS π«Ά
## Creates tmp_dir under $ENV{TEMP} if defined, otherwise /tmp.
use MCE::Signal;
## Attempts to create tmp_dir under /dev/shm if writable.
use MCE::Signal qw( -use_dev_shm );
## Keeps tmp_dir after the script terminates.
use MCE::Signal qw( -keep_tmp_dir );
use MCE::Signal qw( -use_dev_shm -keep_tmp_dir );
## MCE loads MCE::Signal by default when not present.
## Therefore, load MCE::Signal first for options to take effect.
use MCE::Signal qw( -keep_tmp_dir -use_dev_shm );
use MCE;
tl;dr β if you want your options to actually count, load MCE::Signal before `MCE**, because MCE will just load it for you otherwise and your flags get ghosted. π
π DESCRIPTION π
So this package sets up $SIG{ ABRT, HUP, INT, PIPE, QUIT, and TERM } to point at stop_and_exit, and it makes a temporary directory for you. Iconic behaviour, honestly.
When the main process or any worker gets one of those signals, it calls stop_and_exit. And stop_and_exit does the whole routine: it signals every worker to terminate, it removes the temporary directory unless you passed -keep_tmp_dir, and then it terminates itself. periodt. π«‘
Now, where does the temp dir actually live? Listen up:
- It sits under
$ENV{TEMP}if that's defined. - Otherwise it goes under
/dev/shmβ but only if that's writeable and you specified-use_dev_shm. - Otherwise it's
/tmp. - On Windows, the temp directory is made under
$ENV{TEMP}/Perl-MCE/.
As of MCE 1.405, MCE::Signal does not call setpgrp by default anymore. If you want setpgrp, you gotta pass the -setpgrp option to MCE::Signal.
## Running MCE through Daemon::Control requires setpgrp to be called
## for MCE releases 1.511 and below.
use MCE::Signal qw(-setpgrp); ## Not necessary for MCE 1.512 and above
use MCE;
The following are available options and their meanings. π§
-keep_tmp_dirβ the temporary directory is not removed during exiting. A message is displayed with the location afterwards. (she keeps it. she's sentimental. π)-use_dev_shmβ create the temporary directory under/dev/shm.-no_kill9β do notkill -9after receiving a signal to terminate. π-setpgrpβ callssetpgrpto set the process group for the process. This option ensures all workers terminate when reading STDIN for MCE releases1.511and below.
cat big_input_file | ./mce_script.pl | head -10
This works fine without the -setpgrp option:
./mce_script.pl < big_input_file | head -10
lowkey important: nothing is exported by default. Exportable are 1 variable and 2 subroutines. that's it. that's the whole menu. π
use MCE::Signal qw( $tmp_dir stop_and_exit sys_cmd );
use MCE::Signal qw( :all );
$tmp_dirβ Path to the temporary directory.stop_and_exitβ Described below β¬οΈsys_cmdβ Described below β¬οΈ
π stop_and_exit ( [ $exit_status | $signal ] ) π
Stops execution, removes the temp directory, and exits the entire application. Pass 'INT' to terminate a spawned or running MCE session.
MCE::Signal::stop_and_exit(1);
MCE::Signal::stop_and_exit('INT');
π¦ sys_cmd ( $command ) π¦
okay so here's the tea β β the system function in Perl ignores SIGINT and SIGQUIT. Those 2 signals get sent to the command being executed via system() but they do not come back to the underlying Perl script. Which is a whole problem. So sys_cmd was added to MCE::Signal to fix exactly that. π
## Execute command and return the actual exit status. The perl script
## is also signaled if command caught SIGINT or SIGQUIT.
use MCE::Signal qw(sys_cmd); ## Include before MCE
use MCE;
my $exit_status = sys_cmd($command);
π DEFER SIGNAL π
β¨ defer ( $signal ) β¨
Returns immediately inside a signal handler if signaled during IPC. The signal is deferred momentarily and re-signaled automatically upon completing IPC. Currently, all IPC related methods in "MCE::Shared" and one method "send2" in "MCE::Channel" set the flag $MCE::Signal::IPC before initiating IPC.
Current API available since 1.863. π
sub sig_handler {
return MCE::Signal::defer($_[0]) if $MCE::Signal::IPC;
...
}
In a nutshell, "defer" helps safeguard IPC from stalling between workers and the shared manager-process. The following is a demonstration for Unix platforms. Deferring the signal inside the "WINCH" handler prevents the app from eventually failing while resizing the window. (resize your terminal aggressively, bestie, and she still holds it together π)
use strict;
use warnings;
use MCE::Hobo;
use MCE::Shared;
use Time::HiRes 'sleep';
my $count = MCE::Shared->scalar(0);
my $winch = MCE::Shared->scalar(0);
my $done = MCE::Shared->scalar(0);
$SIG{WINCH} = sub {
# defer signal if signaled during IPC
return MCE::Signal::defer($_[0]) if $MCE::Signal::IPC;
# mask signal handler
local $SIG{$_[0]} = 'IGNORE';
printf "inside winch handler %d\n", $winch->incr;
};
$SIG{INT} = sub {
# defer signal if signaled during IPC
return MCE::Signal::defer($_[0]) if $MCE::Signal::IPC;
# set flag for workers to leave loop
$done->set(1);
};
sub task {
while ( ! $done->get ) {
$count->incr;
sleep 0.03;
};
}
print "Resize the terminal window continuously.\n";
print "Press Ctrl-C to stop.\n";
MCE::Hobo->create('task') for 1..8;
sleep 0.015 until $done->get;
MCE::Hobo->wait_all;
printf "\ncount incremented %d times\n\n", $count->get;
π INDEX π
MCEMCE::Core
π AUTHOR π
Mario E. Roy, <marioeroy AT gmail DOT com> π«Ά
okay that's the whole page bestie, you ate it, go make some temp dirs responsibly. π β¨
this was perl v5.42.0, dated 2025-09-10, and it's the manual page for MCE::Signal(3) β mwah, bye π
π¦ where this page comes from
- fedora 43: perl-MCE 1.902-1.fc43 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms