Code: Select all
try:
if os.name == 'nt':
signum = signal.CTRL_BREAK_EVENT
else:
signum = signal.SIGINT
os.kill(pid, signum)
except Exception:
dispynode_logger.debug(traceback.format_exc())
Code: Select all
import multiprocessing
import os
import signal
import time
def do_nothing():
for _ in range:
time.sleep(.2)
if __name__ == "__main__":
p = multiprocessing.Process(target=do_nothing)
p.start()
print(f"Is alive: {p.is_alive()}")
os.kill(p.pid, signal.CTRL_BREAK_EVENT)
time.sleep(1)
print(f"Is alive: {p.is_alive()}")
p.terminate()
# Main thread should still work and print numbers from 0 to 999
for _ in range(1000):
print(i)
time.sleep(.2)
# Output:
# Is alive: True
# Is alive: True
# 0
# Process finished with exit code -1073741510 (0xC000013A interrupted by Ctrl+C)
If we replace CTRL_BREAK_EVENT to SIGTERM then everything works fine, except that we need to manually kill childs:
Code: Select all
if os.name == 'nt' and psutil:
proc = psutil.Process(pid)
dispynode_logger.info("Terminating childs for process with pid: %s", pid)
for child in proc.children(recursive=True):
child.kill()
try:
if os.name == 'nt':
signum = signal.SIGTERM
else:
signum = signal.SIGINT
os.kill(pid, signum)
except Exception:
dispynode_logger.debug(traceback.format_exc())