我正在寻找将 uvicorn.run() 与 FastAPI 应用程序一起使用但没有 uvicorn.run() 阻塞线程的可能性。我已经尝试使用进程、子进程和线程,但没有任何效果。我的问题是我想从另一个进程启动服务器,该进程在启动服务器后应该继续执行其他任务。另外,我在从另一个进程关闭服务器时遇到了问题。
有没有人知道如何使用 uvicorn.run() 非阻塞以及如何从另一个进程中阻止它?
问候 LeukoClassic
当我使用 uvicorn 运行我的 FastAPI 服务器时:
uvicorn main:app --host 0.0.0.0 --port 8000 --log-level info
Run Code Online (Sandbox Code Playgroud)
运行服务器后得到的日志:
INFO: Started server process [405098]
INFO: Waiting for application startup.
INFO: Connect to database...
INFO: Successfully connected to the database!
INFO: Application startup complete.
INFO: Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)
INFO: 122.179.31.158:54604 - "GET /api/hello_world?num1=5&num2=10 HTTP/1.1" 200 OK
Run Code Online (Sandbox Code Playgroud)
如何获取时间戳和请求日志记录?喜欢:
INFO: "2020-07-16:23:34:78" - 122.179.31.158:54604 - "GET /api/hello_world?num1=5&num2=10 HTTP/1.1" 200 OK
Run Code Online (Sandbox Code Playgroud) 在重新加载带有--reload标志的脚本时,是否可以以某种方式排除代码的某些部分?
uvicorn main:app --reload
Run Code Online (Sandbox Code Playgroud)
用例:我有一个需要大量时间加载的模型,所以我想知道是否有办法在重新加载时忽略该行代码。或者这是不可能的?
我有一台在本地运行的服务器。当我在 AWS EC2 上运行它并在端口 8000 上从外部发送请求时,我收到以下错误:
$ uvicorn sql_app.main:app --host="0.0.0.0" --port=8000
INFO: Started server process [9806]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)
WARNING: Invalid HTTP request received.
Traceback (most recent call last):
File "/home/ec2-user/.local/lib/python3.7/site-packages/uvicorn/protocols/http/h11_impl.py", line 170, in handle_events
event = self.conn.next_event()
File "/home/ec2-user/.local/lib/python3.7/site-packages/h11/_connection.py", line 443, in next_event
exc._reraise_as_remote_protocol_error()
File "/home/ec2-user/.local/lib/python3.7/site-packages/h11/_util.py", line 76, in _reraise_as_remote_protocol_error
raise self
File "/home/ec2-user/.local/lib/python3.7/site-packages/h11/_connection.py", line 425, in next_event
event = self._extract_next_receive_event()
File "/home/ec2-user/.local/lib/python3.7/site-packages/h11/_connection.py", line …Run Code Online (Sandbox Code Playgroud) 我已经尝试了一切:
@斯塔莱特:
routes = [
Mount("/static/", StaticFiles(directory=parent+fs+"decoration"+fs+"static"), name="static"),
Route(....),
Route(....),
]
Run Code Online (Sandbox Code Playgroud)
@Uvicorn:
--forwarded-allow-ips=domain.com
--proxy-headers
Run Code Online (Sandbox Code Playgroud)
@url_for:
_external=True
_scheme="https"
Run Code Online (Sandbox Code Playgroud)
@nginx:
proxy_set_header Subdomain $subdomain;
proxy_set_header Host $http_host;
proxy_pass http://localhost:7000/;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Host $server_name;
proxy_redirect http://$http_host/ https://$http_host/;
include proxy_params;
server {
if ($host = sub.domain.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80 ;
listen [::]:80 ;
server_name sub.domain.com;
return 404; # managed by Certbot
}
Run Code Online (Sandbox Code Playgroud)
如果我打开 .css 或 .js …
在这里我想问你,用python运行gunicorn uvicorn和从tiangolo默认运行有什么区别?
我尝试使用JMeter线程属性对这些进行压力测试:

从这些,我得到了结果::

从上面我尝试过:
这是我的案例 1(Tiangolo 基础)的 Dockerfile:
FROM tiangolo/uvicorn-gunicorn-fastapi:python3.8-slim
RUN apt-get update && apt-get install wget gcc -y
RUN mkdir -p /app
WORKDIR /app
COPY ./requirements.txt /app/requirements.txt
RUN python -m pip install --upgrade pip
RUN pip install --no-cache-dir -r /app/requirements.txt
COPY . /app
Run Code Online (Sandbox Code Playgroud)
这是我的 Dockerfile 案例 2(带有 gunicorn 命令的 Python 基础):
FROM python:3.8-slim-buster as builder
RUN apt-get update …Run Code Online (Sandbox Code Playgroud) 我试图了解 FastAPI/Gunicorn 如何处理和排队请求。
客户端合约:客户端发出请求后,需要在 5 秒内收到响应。如果客户端在 5 秒内没有收到响应,则该请求不再有用,客户端将停止监听响应。
场景: 考虑以下场景:
问题:
我有一个 FastAPI 应用程序,其中基于数据库配置动态生成路由。
但是,一旦定义了路由并且应用程序运行,如果配置发生更改,似乎无法重新加载配置以便路由可以反映配置。我现在唯一的解决方案是通过重新启动 uvicorn 来手动重新启动 asgi 应用程序。
有没有什么方法可以在不停止应用程序的情况下完全重新生成路由,最好可以从 URL 调用?
我已经使用异步实现了所有路线。并遵循 FastAPI 文档中的所有准则。
每个路由都有多个数据库调用,没有异步支持,所以它们是这样的正常功能
def db_fetch(query):
# I take a few seconds to respond
return
Run Code Online (Sandbox Code Playgroud)
为了避免阻塞我的事件循环,我使用fastapi.concurrancy.run_in_threadpool
现在的问题是,当大量请求到来时,我的新请求就会被阻止。即使我关闭浏览器选项卡(取消请求),整个应用程序也会卡住,直到处理旧的请求为止。
我在这里做错了什么?
我用它uvicorn作为我的 ASGI 服务器。我在具有 2 个副本的 kubernetes 集群中运行。
很少有人怀疑:我是否产生了太多线程?这是 uvicron 中的一些错误吗?不太确定!
我有 FastAPI (Python) 和 uvicorn[标准]。我有这个错误:
error walking file system: OSError [Errno 40] Too many levels of symbolic links: '/sys/class/vtconsole/vtcon0/subsystem/vtcon0/subsystem/vtcon0/subsystem/vtcon0/subsystem/vtcon0/subsystem/vtcon0/subsystem/vtcon0/subsystem/vtcon0/subsystem/vtcon0/subsystem/vtcon0/subsystem/vtcon0/subsystem/vtcon0/subsystem/vtcon0/subsystem/vtcon0/subsystem/vtcon0/subsystem/vtcon0/subsystem/vtcon0/subsystem/vtcon0/subsystem/vtcon0/subsystem/vtcon0/subsystem/vtcon0'
Run Code Online (Sandbox Code Playgroud)
如果我只使用 uvicorn,一切都可以,但我需要 uvicorn[standard]。如何修复它?
我在 Docker 中使用它。
uvicorn ×10
fastapi ×9
python ×6
gunicorn ×2
starlette ×2
docker ×1
nginx ×1
python-3.x ×1
python-anyio ×1