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 的元组列表中对每个元组中的第一个值求和? 2022-01-01
- python-m http.server 443--使用SSL? 2022-01-01
- 分析异常:路径不存在:dbfs:/databricks/python/lib/python3.7/site-packages/sampleFolder/data; 2022-01-01
- pytorch 中的自适应池是如何工作的? 2022-07-12
- padding='same' 转换为 PyTorch padding=# 2022-01-01
- 如何将一个类的函数分成多个文件? 2022-01-01
- 使用Heroku上托管的Selenium登录Instagram时,找不到元素';用户名'; 2022-01-01
- 如何在 python3 中将 OrderedDict 转换为常规字典 2022-01-01
- 沿轴计算直方图 2022-01-01
- python check_output 失败,退出状态为 1,但 Popen 适用于相同的命令 2022-01-01