python 清理 subprocess 的子进程 使用了参数 shell=True
【代码】python 清理 subprocess 的子进程(当 shell=True 时)
·
对于一般的 直接调用 process.kill() 即可 ,当使用参数 shell=True时:
process = subprocess.Popen('xx xx xx xx‘, shell=True)
if os.name == 'nt': # windows
subprocess.Popen("TASKKILL /F /PID {pid} /T".format(pid=process.pid))
else:
os.kill(process.pid, signal.SIGTERM)
其他:
使用 psutil 查询本机监听某个端口的进程:
def get_process_by_port(port):
connections = psutil.net_connections()
for connection in connections:
if connection.status == 'LISTEN' and connection.laddr.port == port:
return psutil.Process(connection.pid)
return None

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐
所有评论(0)