如何关闭在Windows上运行其他子过程的子过程?
我面临以下问题,无法解决。
- a 运行 b AS
Multiprocessing.process()
- b b 运行 c as
subprocess.run()
- a terminates b by
multiprocessing.process.process.ternication.terminate(
- ) b>和 c 被终止
结果: b 被终止, c 在后台运行
重要的附加信息
- a , b , c 是程序
- a 是我的程序,我开发
- b , c 是我不是由我开发的外部程序我没有控制 解决方案
- 必须在 Windows 问题上工作
是否有人有任何解决方案或知道是否不可能?
以下代码在示例中描述了我的问题:
# main.py - Process A
# Runs Process B
import multiprocessing
from Manager import manager
from time import sleep
def main():
proc = multiprocessing.Process(target=manager)
proc.start()
print("[MAIN] Process going sleep")
sleep(3)
print("[MAIN] Process is awake now. Beginning to terminate")
proc.terminate()
proc.join()
print("[MAIN] Process Manager terminated.")
if __name__ == "__main__":
main()
# Manager.py - Process B
# Cannot change any code
# Runs process C
import subprocess
def manager():
print("[MANAGER] START")
subprocess.run(["python", "sleepery.py"])
print("[MANAGER] Terminated")
# sleepery.py - Process C
# Cannot change any code
from time import sleep
def sleeper():
"""This process prints info every two seconds for 8"""
for _ in range(4):
sleep(2)
print(f"[SLEEPER] prints!")
print("[SLEEPER] ends")
if __name__ == "__main__":
sleeper()
输出
[MAIN] Process going sleep
[MANAGER] START
[SLEEPER] prints!
[MAIN] Process is awake now. Beginning to terminate
[MAIN] Process Manager terminated.
[SLEEPER] prints!
[SLEEPER] prints!
[SLEEPER] prints!
[SLEEPER] ends
想要输出
[MAIN] Process going sleep
[MANAGER] START
[SLEEPER] prints!
[MAIN] Process is awake now. Beginning to terminate
[MAIN] Process Manager terminated.
我的研究
唯一有效的是通过Taskskill
杀死所有Python进程 但这并不完全是优雅的
I am facing the following problem and can't solve it.
- A runs B as
multiprocessing.Process()
- B runs C as
subprocess.run()
- A terminates B by
multiprocessing.Process.terminate()
- Expected: B and C are terminated
Result: B is terminated and C runs in background
Important additional information
- A, B, C are programs
- A is my program that I develop
- B, C are external programs not developed by me and I have no control
of - Solution must work on Windows
Question Does anyone have any solution or know if it isn't possible?
Following code describes my problem on example:
# main.py - Process A
# Runs Process B
import multiprocessing
from Manager import manager
from time import sleep
def main():
proc = multiprocessing.Process(target=manager)
proc.start()
print("[MAIN] Process going sleep")
sleep(3)
print("[MAIN] Process is awake now. Beginning to terminate")
proc.terminate()
proc.join()
print("[MAIN] Process Manager terminated.")
if __name__ == "__main__":
main()
# Manager.py - Process B
# Cannot change any code
# Runs process C
import subprocess
def manager():
print("[MANAGER] START")
subprocess.run(["python", "sleepery.py"])
print("[MANAGER] Terminated")
# sleepery.py - Process C
# Cannot change any code
from time import sleep
def sleeper():
"""This process prints info every two seconds for 8"""
for _ in range(4):
sleep(2)
print(f"[SLEEPER] prints!")
print("[SLEEPER] ends")
if __name__ == "__main__":
sleeper()
OUTPUT
[MAIN] Process going sleep
[MANAGER] START
[SLEEPER] prints!
[MAIN] Process is awake now. Beginning to terminate
[MAIN] Process Manager terminated.
[SLEEPER] prints!
[SLEEPER] prints!
[SLEEPER] prints!
[SLEEPER] ends
WANTED OUTPUT
[MAIN] Process going sleep
[MANAGER] START
[SLEEPER] prints!
[MAIN] Process is awake now. Beginning to terminate
[MAIN] Process Manager terminated.
My research
The only thing that works is to kill all python processes by taskkill
But it isn't exactly graceful
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 job对象,这允许您分组这项工作中的所有儿童流程都在一步中杀死他们。
Use a Job object, this allows you to group all child processes in this job and kill them all in a single step.
安德斯的答案直接进入了这一点。
这是重构 main.py 使用作业对象,
然后输出:
Anders's answer goes right into the point.
Here is refactored main.py to use Job Objects
Then output: