1MCE::Signal(3) User Contributed Perl Documentation MCE::Signal(3)
2
3
4
6 MCE::Signal - Temporary directory creation/cleanup and signal handling
7
9 This document describes MCE::Signal version 1.874
10
12 ## Creates tmp_dir under $ENV{TEMP} if defined, otherwise /tmp.
13
14 use MCE::Signal;
15
16 ## Attempts to create tmp_dir under /dev/shm if writable.
17
18 use MCE::Signal qw( -use_dev_shm );
19
20 ## Keeps tmp_dir after the script terminates.
21
22 use MCE::Signal qw( -keep_tmp_dir );
23 use MCE::Signal qw( -use_dev_shm -keep_tmp_dir );
24
25 ## MCE loads MCE::Signal by default when not present.
26 ## Therefore, load MCE::Signal first for options to take effect.
27
28 use MCE::Signal qw( -keep_tmp_dir -use_dev_shm );
29 use MCE;
30
32 This package configures $SIG{ HUP, INT, PIPE, QUIT, and TERM } to point
33 to stop_and_exit and creates a temporary directory. The main process
34 and workers receiving said signals call stop_and_exit, which signals
35 all workers to terminate, removes the temporary directory unless
36 -keep_tmp_dir is specified, and terminates itself.
37
38 The location of the temp directory resides under $ENV{TEMP} if defined,
39 otherwise /dev/shm if writeable and -use_dev_shm is specified, or /tmp.
40 On Windows, the temp directory is made under $ENV{TEMP}/Perl-MCE/.
41
42 As of MCE 1.405, MCE::Signal no longer calls setpgrp by default. Pass
43 the -setpgrp option to MCE::Signal to call setpgrp.
44
45 ## Running MCE through Daemon::Control requires setpgrp to be called
46 ## for MCE releases 1.511 and below.
47
48 use MCE::Signal qw(-setpgrp); ## Not necessary for MCE 1.512 and above
49 use MCE;
50
51 The following are available options and their meanings.
52
53 -keep_tmp_dir - The temporary directory is not removed during exiting
54 A message is displayed with the location afterwards
55
56 -use_dev_shm - Create the temporary directory under /dev/shm
57 -no_kill9 - Do not kill -9 after receiving a signal to terminate
58
59 -setpgrp - Calls setpgrp to set the process group for the process
60 This option ensures all workers terminate when reading
61 STDIN for MCE releases 1.511 and below.
62
63 cat big_input_file | ./mce_script.pl | head -10
64
65 This works fine without the -setpgrp option:
66
67 ./mce_script.pl < big_input_file | head -10
68
69 Nothing is exported by default. Exportable are 1 variable and 2
70 subroutines.
71
72 use MCE::Signal qw( $tmp_dir stop_and_exit sys_cmd );
73 use MCE::Signal qw( :all );
74
75 $tmp_dir - Path to the temporary directory.
76 stop_and_exit - Described below
77 sys_cmd - Described below
78
79 stop_and_exit ( [ $exit_status | $signal ] )
80 Stops execution, removes temp directory, and exits the entire
81 application. Pass 'INT' to terminate a spawned or running MCE session.
82
83 MCE::Signal::stop_and_exit(1);
84 MCE::Signal::stop_and_exit('INT');
85
86 sys_cmd ( $command )
87 The system function in Perl ignores SIGINT and SIGQUIT. These 2 signals
88 are sent to the command being executed via system() but not back to the
89 underlying Perl script. For this reason, sys_cmd was added to
90 MCE::Signal.
91
92 ## Execute command and return the actual exit status. The perl script
93 ## is also signaled if command caught SIGINT or SIGQUIT.
94
95 use MCE::Signal qw(sys_cmd); ## Include before MCE
96 use MCE;
97
98 my $exit_status = sys_cmd($command);
99
101 defer ( $signal )
102 Returns immediately inside a signal handler if signaled during IPC.
103 The signal is deferred momentarily and re-signaled automatically upon
104 completing IPC. Currently, all IPC related methods in "MCE::Shared" and
105 one method "send2" in "MCE::Channel" set the flag $MCE::Signal::IPC
106 before initiating IPC.
107
108 Current API available since 1.863.
109
110 sub sig_handler {
111 return MCE::Signal::defer($_[0]) if $MCE::Signal::IPC;
112 ...
113 }
114
115 In a nutshell, "defer" helps safeguard IPC from stalling between
116 workers and the shared manager-process. The following is a
117 demonstration for Unix platforms. Deferring the signal inside the
118 "WINCH" handler prevents the app from eventually failing while resizing
119 the window.
120
121 use strict;
122 use warnings;
123
124 use MCE::Hobo;
125 use MCE::Shared;
126 use Time::HiRes 'sleep';
127
128 my $count = MCE::Shared->scalar(0);
129 my $winch = MCE::Shared->scalar(0);
130 my $done = MCE::Shared->scalar(0);
131
132 $SIG{WINCH} = sub {
133 # defer signal if signaled during IPC
134 return MCE::Signal::defer($_[0]) if $MCE::Signal::IPC;
135
136 # mask signal handler
137 local $SIG{$_[0]} = 'IGNORE';
138
139 printf "inside winch handler %d\n", $winch->incr;
140 };
141
142 $SIG{INT} = sub {
143 # defer signal if signaled during IPC
144 return MCE::Signal::defer($_[0]) if $MCE::Signal::IPC;
145
146 # set flag for workers to leave loop
147 $done->set(1);
148 };
149
150 sub task {
151 while ( ! $done->get ) {
152 $count->incr;
153 sleep 0.03;
154 };
155 }
156
157 print "Resize the terminal window continuously.\n";
158 print "Press Ctrl-C to stop.\n";
159
160 MCE::Hobo->create('task') for 1..8;
161 sleep 0.015 until $done->get;
162 MCE::Hobo->wait_all;
163
164 printf "\ncount incremented %d times\n\n", $count->get;
165
167 MCE, MCE::Core
168
170 Mario E. Roy, <marioeroy AT gmail DOT com>
171
172
173
174perl v5.32.0 2020-08-19 MCE::Signal(3)