我真的不明白http请求是怎么回事。当我启动子进程和uvicorn timeout_keep_alive
超时时,我尝试在浏览器中点击“停止”,得到无限加载和没有HTTP连接日志。但是,如果我尝试单击其他按钮或刷新页面,它可以工作,我会得到两个响应。
Netstat显示端口60862
上的套接字绑定是打开的,但是uvicorn日志:
TRACE: 127.0.0.1:60862 - HTTP connection lost
。
import multiprocessing
import os
import time
import uvicorn
from fastapi import FastAPI
app = FastAPI()
processes = []
def keep_alive_process():
while True:
print(f"process {os.getpid()} is alive")
time.sleep(1)
@app.post("/start")
async def start_processes():
for i in range(4):
process = multiprocessing.Process(target=keep_alive_process,
args=())
processes.append(process)
process.start()
return {'status': 'started'}
@app.post("/stop")
async def stop_processes():
for process in processes:
process.kill()
processes.clear()
return {'status': 'stopped'}
if __name__ == '__main__':
uvicorn.run('main:app', timeout_keep_alive=10, log_level='trace')
TRACE: 127.0.0.1:60862 - HTTP connection made
TRACE: 127.0.0.1:60862 - ASGI [2] Started scope={'type': 'http', 'asgi': {'version': '3.0', 'spec_version': '2.3'}, 'http_version': '1.1', 'server': ('127.0.0.1', 8010), 'client': ('127.0.0.1', 60862), 'scheme': 'http', 'root_path': '', 'headers': '<...>', 'method': 'POST', 'path': '/start', 'raw_path': b'/start', 'query_string': b''}
TRACE: 127.0.0.1:60862 - ASGI [2] Send {'type': 'http.response.start', 'status': 200, 'headers': '<...>'}
TRACE: 127.0.0.1:60862 - ASGI [2] Send {'type': 'http.response.body', 'body': '<20 bytes>'}
TRACE: 127.0.0.1:60862 - ASGI [2] Completed
process 63912 is alive
process 63913 is alive
process 63914 is alive
process 63915 is alive
INFO: 127.0.0.1:60862 - "POST /start HTTP/1.1" 200 OK
process 63912 is alive
process 63913 is alive
TRACE: 127.0.0.1:60862 - HTTP connection lost
....
process 63912 is alive
INFO: 127.0.0.1:59092 - "POST /stop HTTP/1.1" 200 OK
INFO: 127.0.0.1:59092 - "POST /stop HTTP/1.1" 200 OK
TRACE: 127.0.0.1:59092 - HTTP connection made
TRACE: 127.0.0.1:59092 - ASGI [3] Started scope={'type': 'http', 'asgi': {'version': '3.0', 'spec_version': '2.3'}, 'http_version': '1.1', 'server': ('127.0.0.1', 8010), 'client': ('127.0.0.1', 59092), 'scheme': 'http', 'root_path': '', 'headers': '<...>', 'method': 'POST', 'path': '/stop', 'raw_path': b'/stop', 'query_string': b''}
TRACE: 127.0.0.1:59092 - ASGI [3] Send {'type': 'http.response.start', 'status': 200, 'headers': '<...>'}
TRACE: 127.0.0.1:59092 - ASGI [3] Send {'type': 'http.response.body', 'body': '<20 bytes>'}
TRACE: 127.0.0.1:59092 - ASGI [3] Completed
TRACE: 127.0.0.1:59092 - ASGI [4] Started scope={'type': 'http', 'asgi': {'version': '3.0', 'spec_version': '2.3'}, 'http_version': '1.1', 'server': ('127.0.0.1', 8010), 'client': ('127.0.0.1', 59092), 'scheme': 'http', 'root_path': '', 'headers': '<...>', 'method': 'POST', 'path': '/stop', 'raw_path': b'/stop', 'query_string': b''}
TRACE: 127.0.0.1:59092 - ASGI [4] Send {'type': 'http.response.start', 'status': 200, 'headers': '<...>'}
TRACE: 127.0.0.1:59092 - ASGI [4] Send {'type': 'http.response.body', 'body': '<20 bytes>'}
TRACE: 127.0.0.1:59092 - ASGI [4] Completed
TRACE: 127.0.0.1:59092 - HTTP connection lost
发布于 2022-10-05 15:51:33
因此,我唯一的决定(解决办法)是将timeout_keep_alive uvicorn param设置为零,并使用Fastapi的BackgroundTask。套接字在发送响应后立即关闭,但进程在发送响应后产生。
https://stackoverflow.com/questions/73940652
复制相似问题