Python-如何使用FastAPI和uvicorn.run而不阻塞线程?

问题描述

我正在寻找将uvicorn.run()与FastAPI应用程序一起使用的可能性,但没有uvicorn.run()会阻塞线程。我已经尝试使用进程、子进程和线程,但都不起作用。 我的问题是,我想从另一个进程启动服务器,该进程应该在启动服务器之后继续执行其他任务。此外,我在从另一个进程以这种方式关闭服务器时遇到问题。

有人知道如何使用uvicorn.run()非阻塞,以及如何从另一个进程停止它吗?

问候语 LeukoClassic


解决方案

根据Uvicorn文档,没有以编程方式停止服务器的方法。 相反,您只能通过按ctrl+c(正式)来停止服务器。

但我有一个技巧,可以使用multiprocessing标准库和以下三个简单函数以编程方式解决此问题:

  • 用于运行服务器的Run函数。
  • 启动新进程(启动服务器)的启动函数。
  • 加入进程(停止服务器)的停止函数。
from multiprocessing import Process
import uvicorn

# global process variable
proc = None


def run(): 
    """
    This function to run configured uvicorn server.
    """
    uvicorn.run(app=app, host=host, port=port)


def start():
    """
    This function to start a new process (start the server).
    """
    global proc
    # create process instance and set the target to run function.
    # use daemon mode to stop the process whenever the program stopped.
    proc = Process(target=run, args=(), daemon=True)
    proc.start()


def stop(): 
    """
    This function to join (stop) the process (stop the server).
    """
    global proc
    # check if the process is not None
    if proc: 
        # join (stop) the process with a timeout setten to 0.25 seconds.
        # using timeout (the optional arg) is too important in order to
        # enforce the server to stop.
        proc.join(0.25)


您可以:

  • 使用threading标准库,而不是使用multiprocessing标准库。

  • 将这些函数重构为一个类。


用法示例:

from time import sleep

if __name__ == "__main__":
    # to start the server call start function.
    start()
    # run some codes ....
    # to stop the server call stop function.
    stop()



您可以阅读有关以下内容的更多信息:

  • Uvicorn服务器。
  • multiprocessing标准库
  • threading标准库。
  • Concurrency了解有关python中的多处理和线程的更多信息。

相关文章