Python中Linux下multiprocessing模块如何正确结束子进程?
# -*- coding:utf-8 -*-
import time
import multiprocessing
主进程
def Start(num):
while True:
print(">>>>>>>> 我是第 " + str(num) + " 个子线程!")
time.sleep(20)
if name == ‘main’:
# 创建子进程让定时器多进程同步运行
i = 0
while i < 4:
print(“创建第 " + str(i) + " 子进程!”)
t = multiprocessing.Process(target=Start, args=(i,))
t.start()
i = i + 1
time.sleep(5) # 给个延时,避免子线程启动太快导致的错误
我有一个问题就是,这样创建子进程,然后我如何在 linux 下 kill 掉主进程的 pid 之后,子进程都能结束呢?
我百度了很多文章,都没看明白人家写的意思;
所以看看大神是否能解答一下,我大概在什么位置需要加个什么东西来处理?
Python中Linux下multiprocessing模块如何正确结束子进程?
kill 主进程,子进程的父进程,应该是直接变成 init(pid 1)
在Linux下用multiprocessing结束子进程,最靠谱的方法是设置一个共享的Event或Value作为信号,让子进程自己优雅退出。千万别直接用terminate(),那会强制杀掉进程,可能导致资源没释放。
看这个例子,用Event来通知:
import multiprocessing
import time
import signal
def worker(stop_event):
"""子进程任务"""
print(f"子进程 {multiprocessing.current_process().pid} 启动")
try:
while not stop_event.is_set():
# 模拟工作
print(f"进程 {multiprocessing.current_process().pid} 工作中...")
time.sleep(1)
except KeyboardInterrupt:
print(f"进程 {multiprocessing.current_process().pid} 收到中断")
finally:
print(f"子进程 {multiprocessing.current_process().pid} 清理退出")
if __name__ == '__main__':
# 创建停止事件
stop_event = multiprocessing.Event()
# 创建进程
processes = []
for i in range(3):
p = multiprocessing.Process(target=worker, args=(stop_event,))
p.start()
processes.append(p)
try:
# 主进程等待
time.sleep(5)
print("\n主进程发送停止信号...")
stop_event.set() # 设置停止事件
# 等待子进程结束
for p in processes:
p.join(timeout=2)
if p.is_alive():
print(f"进程 {p.pid} 超时,强制终止")
p.terminate()
except KeyboardInterrupt:
print("\n收到Ctrl+C,发送停止信号...")
stop_event.set()
for p in processes:
p.join(timeout=1)
finally:
# 确保所有进程都结束了
for p in processes:
if p.is_alive():
p.terminate()
p.join()
print("所有子进程已结束")
关键点:
- 用Event做信号:主进程
stop_event.set()通知所有子进程 - 子进程循环检查:
while not stop_event.is_set()让子进程有机会清理 - 设置join超时:给子进程时间优雅退出,超时才用
terminate() - 处理KeyboardInterrupt:捕获Ctrl+C,通知子进程退出
如果子进程里有阻塞操作(比如queue.get()),用timeout参数:
while not stop_event.is_set():
try:
item = queue.get(timeout=1.0) # 设置超时,定期检查stop_event
# 处理item
except queue.Empty:
continue
简单说就是发信号让子进程自己退出,别硬杀。
不要老盯着 python
好好把 linux fork wait 信号部分学一下
你这些问题光在 python 里是解答不了的
linux 里父进程结束 子进程不一定结束啊 信号处理都是单独的 你可以试试在父进程添加信号处理函数 接收到某个信号的时候就杀死全部子进程然后父进程自己退出 这样应该可以
https://docs.python.org/3.6/library/signal.html
你需要一个 singal handler,
import singal
类似于
def int_singnal_handler(signum, frame):
pass # do something to end your processes
# blah blah blah
signal.signal(signal.SIGINT, int_signal_handler)
嗯
这问题简直太经典了
“单看问题”就能得出没好好读书的结论
不要酱紫。
怎么输出里又有线程?
在 t.start() 前设置:t.daemon=True
比较好的做法就是
父进程开一个管道
子进程开一个线程去阻塞读这个管道, 一旦读到就 os._exit
4 楼√
你这个方法是可行的。可是发现一个问题。如果 TTY 是一个问号。。就是假如这个脚本是由另外一个脚本发起运行的。tty 就会变成问号。然后 os.killpg 就会报错不存在这个进程。
4 楼√
man fork
* The prctl(2) PR_SET_PDEATHSIG setting is reset so that the child does not receive a signal when its parent terminates.
845 是 pid 啊
os.killpg(os.getpgid(p.pid), signal.SIGKILL)
=========================
os.killpg(pgid, sig)
Send the signal sig to the process group pgid.
--------------------------------
os.kill(pid, sig)
Send signal sig to the process pid. Constants for the specific signals available on the host platform are defined in the signal module.
t.daeman =true
老大做完,小弟就不做了
t.join()
老大等小弟做完继续在做
class CountdownTask:
def init(self):
self._running = True
def terminate(self):
self._running = False
def run(self, n):
while self._running and n > 0:
print(‘T-minus’, n)
n -= 1
time.sleep(5)
c = CountdownTask()
t = multiprocessing.Process(target=c.run, args=(10,))
t.start()
c.terminate() # Signal termination
t.join() # Wait for actual termination (if needed)
老大给小弟给信号
不知道理解的对不对、、、
<br>class CountdownTask:<br> def __init__(self):<br> self._running = True<br><br> def terminate(self):<br> self._running = False<br><br> def run(self, n):<br> while self._running and n > 0:<br> print('T-minus', n)<br> n -= 1<br> time.sleep(5)<br><br>c = CountdownTask()<br>t = Thread(target=<a target="_blank" href="http://c.run" rel="nofollow noopener">c.run</a>, args=(10,))<br>t.start()<br>c.terminate() # Signal termination<br>t.join() # Wait for actual termination (if needed)<br>

