Set the "death signal" of the current process, so that the current process will be cleaned with guarantee in case the parent dies accidentally.
(_warn=True)
| 170 | |
| 171 | |
| 172 | def enable_death_signal(_warn=True): |
| 173 | """ |
| 174 | Set the "death signal" of the current process, so that |
| 175 | the current process will be cleaned with guarantee |
| 176 | in case the parent dies accidentally. |
| 177 | """ |
| 178 | if platform.system() != 'Linux': |
| 179 | return |
| 180 | try: |
| 181 | import prctl # pip install python-prctl |
| 182 | except ImportError: |
| 183 | if _warn: |
| 184 | log_once('"import prctl" failed! Install python-prctl so that processes can be cleaned with guarantee.', |
| 185 | 'warn') |
| 186 | return |
| 187 | else: |
| 188 | assert hasattr(prctl, 'set_pdeathsig'), \ |
| 189 | "prctl.set_pdeathsig does not exist! Note that you need to install 'python-prctl' instead of 'prctl'." |
| 190 | # is SIGHUP a good choice? |
| 191 | prctl.set_pdeathsig(signal.SIGHUP) |
| 192 | |
| 193 | |
| 194 | def is_main_thread(): |