fastapi多线程非阻塞启动

发布时间 2023-03-22 21:11:30作者: selfcs

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后,程序会阻塞,如下:

image-20230321174738597

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("你要执行的其他程序")