1 问题描述
我在run.py
文件下的主函数如下所示:
import uvicorn
from fastapi import FastAPI
app = FastAPI(
title="chatglm",
description="开源版的chatglm接口",
version="1.0.0",
)
if __name__ == '__main__':
uvicorn.run(
"run:app", host="0.0.0.0", port=9899
)
运行run.py
后,程序会阻塞,如下:
在uvicorn
这行代码之后,其他的程序、函数都不会执行,因为阻塞了。
2 解决方法
使用多线程的方法。具体如下:
import time
import uvicorn
import contextlib
import threading
class UvicornServer(uvicorn.Server):
def install_signal_handlers(self):
pass
@contextlib.contextmanager
def run_in_thread(self):
thread = threading.Thread(target=self.run)
thread.start()
try:
while not self.started:
time.sleep(1e-3)
yield
finally:
self.should_exit = True
thread.join()
if __name__ == '__main__':
config = uvicorn.Config("run:app", host="0.0.0.0", port=9899, log_level="info")
server = UvicornServer(config=config)
with server.run_in_thread():
print("你要执行的其他程序")
标签:__,run,thread,fastapi,self,阻塞,uvicorn,import,多线程
From: https://www.cnblogs.com/selfcs/p/17240902.html