我想杀死整个进程树.使用任何常用脚本语言执行此操作的最佳方法是什么?我正在寻找一个简单的解决方案.
我有一个用 Uvicorn + FastAPI 编写的应用程序。我正在使用 PyTest 测试响应时间。
参考如何在使用 PyTest 进行测试时在后台启动 Uvicorn + FastAPI,我编写了测试。然而,当工人> = 2时,我在完成测试后发现应用程序进程处于活动状态。
我想在测试结束时干净地终止应用程序进程。
你有什么主意吗?
详情如下。
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def hello_world():
return "hello world"
Run Code Online (Sandbox Code Playgroud)
from multiprocessing import Process
import pytest
import requests
import time
import uvicorn
HOST = "127.0.0.1"
PORT = 8765
WORKERS = 1
def run_server(host: str, port: …Run Code Online (Sandbox Code Playgroud)我正在使用 python 中的 subprocess 模块运行一些 shell 脚本。如果 shell 脚本运行时间过长,我喜欢终止子进程。我认为如果我将 传递timeout=30给我的run(..)陈述就足够了。
这是代码:
try:
result=run(['utilities/shell_scripts/{0} {1} {2}'.format(
self.language_conf[key][1], self.proc_dir, config.main_file)],
shell=True,
check=True,
stdout=PIPE,
stderr=PIPE,
universal_newlines=True,
timeout=30,
bufsize=100)
except TimeoutExpired as timeout:
Run Code Online (Sandbox Code Playgroud)
我已经用一些运行 120 秒的 shell 脚本测试了这个调用。我预计子进程会在 30 秒后被终止,但实际上该进程正在完成 120 秒的脚本,然后引发超时异常。现在的问题是如何通过超时终止子进程?