如何在不阻塞线程的情况下使用FastAPI和uvicorn.run? [英] How to use FastAPI and uvicorn.run without blocking the thread?

查看:483
本文介绍了如何在不阻塞线程的情况下使用FastAPI和uvicorn.run?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找在FstAPI应用程序中使用uvicorn.run()的可能性,但没有uvicorn.run()会阻塞线程.我已经尝试使用进程,子进程和线程,但是没有任何效果.我的问题是我想从另一个进程启动服务器,该进程应该在启动服务器后继续执行其他任务.另外,我在从另一个进程中关闭服务器时遇到问题.

I'm looking for a possibility to use uvicorn.run() with a FstAPI app but without uvicorn.run() is blocking the thread. I already tried to use processes, subprocessesand threads but nothing worked. My problem is that I want to start the Server from another process that should go on with other tasks after starting the server. Additinally I have problems closing the server like this from another process.

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

Has anyone an idea how to use uvicorn.run() non blocking and how to stop it from another process?

问候LeukoClassic

Greetings LeukoClassic

推荐答案

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

According to Uvicorn documentation there is no programmatically way to stop the server. instead, you can stop the server only by pressing ctrl + c (officially).

但是我有一个技巧可以使用 multiprocessing 标准以编程方式解决此问题lib具有以下三个简单功能:

But I have a trick to solve this problem programmatically using multiprocessing standard lib with these three simple functions :

  • 用于运行服务器的运行功能.
  • 用于启动新进程(启动服务器)的启动功能.
  • 用于加入进程的停止功能(停止服务器).
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)


同样的想法,您可以:

  • use threading standard lib instead of using multiprocessing standard lib.

将这些函数重构为一个类.

refactor these functions into a class.


用法示例:

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



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



You can read more about :

  • Uvicorn server.
  • multiprocessing standard lib.
  • threading standard lib.
  • Concurrency to know more about multi processing and threading in python.

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

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