Python-如何使用FastAPI和uvicorn.run而不阻塞线程? [英] Python - How to use FastAPI and uvicorn.run without blocking the thread?

查看:200
本文介绍了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)


您可以


用法示例:

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()



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

这篇关于Python-如何使用FastAPI和uvicorn.run而不阻塞线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆