
在 python 中使用信号来终止进程时,常常会出现这样的问题:发出信号后,主进程终止,但子进程仍在运行。这主要是因为信号的传递范围只限于进程,无法跨进程传递。
解决方案:
进程组是一组由主进程及其所有子进程构成的集合,并且可以与信号一起使用。可以通过以下步骤解决问题:
立即学习“Python免费学习笔记(深入)”;
这样,信号将传递给进程组中的所有进程,包括子进程。
信号处理函数的注册必须放置在创建子进程之后,以确保子进程在注册之前已创建。
修改后的代码示例:
a.py:
import multiprocessing
import os
import signal
import time
def process1():
while true:
print("子进程运行中")
time.sleep(1)
def handle_signal(signum, frame):
print(signum)
if signum == signal.sigterm:
print("received sigterm signal. deleting pid file...")
try:
if os.path.exists("./crawler.pid"):
os.remove("./crawler.pid")
print("pid file deleted successfully.")
except exception as e:
print("error deleting pid file:", e)
if __name__ == "__main__":
a = multiprocessing.process(target=process1)
a.daemon = true
a.start()
child_pid = a.pid
parent_pid = os.getpid()
parent_pid_group = os.getpgid(parent_pid)
signal.signal(signal.sigterm, handle_signal)
with open("./crawler.pid", "w") as f:
f.write(str(parent_pid_group))
a.join()
print(f"父进程pid: {parent_pid}")
print(f"子进程pid: {child_pid}")b.py:
import os
import signal
with open("./crawler.pid", "r") as f:
try:
pid = int(f.read())
os.killpg(pid, signal.SIGTERM)
print(f"Signal sent successfully to process {pid}")
except Exception as e:
print(f"Error sending signal: {e}")以上就是Python子进程不随主进程退出怎么办?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号