Python - How to use FastAPI and uvicorn.run without blocking the thread?(Python-如何使用FastAPI和uvicorn.run而不阻塞线程?)
                            本文介绍了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中的多处理和线程的更多信息。
 
这篇关于Python-如何使用FastAPI和uvicorn.run而不阻塞线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
				 沃梦达教程
				
			本文标题为:Python-如何使用FastAPI和uvicorn.run而不阻塞线程?
				
        
 
            
        基础教程推荐
             猜你喜欢
        
	     - 在Python中从Azure BLOB存储中读取文件 2022-01-01
 - 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
 - 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
 - 包装空间模型 2022-01-01
 - 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
 - 修改列表中的数据帧不起作用 2022-01-01
 - PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
 - 求两个直方图的卷积 2022-01-01
 - Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
 - PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
 
    	
    	
    	
    	
    	
    	
    	
    	
				
				
				
				