标签: uvicorn

Python - 如何在不阻塞线程的情况下使用 FastAPI 和 uvicorn.run?

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

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

问候 LeukoClassic

python multiprocessing fastapi uvicorn

7
推荐指数
2
解决办法
8254
查看次数

如何在 uvicorn 日志中为每个请求添加时间戳?

当我使用 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)

python fastapi uvicorn

7
推荐指数
3
解决办法
3142
查看次数

用 uvicorn 重新加载标志:我们可以排除某些代码吗?

在重新加载带有--reload标志的脚本时,是否可以以某种方式排除代码的某些部分?

uvicorn main:app --reload
Run Code Online (Sandbox Code Playgroud)

用例:我有一个需要大量时间加载的模型,所以我想知道是否有办法在重新加载时忽略该行代码。或者这是不可能的?

python fastapi uvicorn

7
推荐指数
1
解决办法
2142
查看次数

使用 uvicorn + fastapi 在 AWS EC2 上出现 uvicorn 错误

我有一台在本地运行的服务器。当我在 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)

python amazon-web-services fastapi uvicorn

7
推荐指数
2
解决办法
3167
查看次数

Starlette 的 url_for 不会在 Nginx 后面创建带有 https 方案的链接(通过 uvicorn)

我已经尝试了一切:

@斯塔莱特:

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 …

nginx starlette uvicorn

7
推荐指数
1
解决办法
1939
查看次数

FastAPI Gunicorn Uvicorn 用于使用 Google Cloud Run 进行生产部署(压力测试)

在这里我想问你,用python运行gunicorn uvicorn和从tiangolo默认运行有什么区别?

我尝试使用JMeter线程属性对这些进行压力测试:

截图来自2021-02-18 12-29-26

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

截图来自 2021-02-18 12-20-05

从上面我尝试过:

  1. 带有 tiangolo 基础的 Dockerfile
  2. 使用 python:3.8-slim-buster 的 Dockerfile 并使用 gunicorn 命令运行它
  3. 使用 python:3.8-slim-buster 的 Dockerfile 并使用 python 运行它

这是我的案例 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)

stress-testing gunicorn fastapi uvicorn

7
推荐指数
2
解决办法
4982
查看次数

FastAPI/Gunicorn 中的请求如何排队?

我试图了解 FastAPI/Gunicorn 如何处理和排队请求。

客户端合约:客户端发出请求后,需要在 5 秒内收到响应。如果客户端在 5 秒内没有收到响应,则该请求不再有用,客户端将停止监听响应。

场景: 考虑以下场景:

  1. 假设我们有 4 个工作人员(使用 Gunicorn 作为 UvicornWorkers 的流程管理器;使用tiangolo 的 docker 镜像
  2. 客户端发送100个并发请求
  3. 每个请求需要 5 秒才能完成
  4. 我们在 5 秒内处理前 4 个请求
  5. 但到那时,队列中接受的剩余 96 个请求(我不确定是否发生排队或它是如何工作的,如果您能解释一下,我将不胜感激)已经在那里花费了 5 秒并违反了客户端合同。处理这 96 个请求是一种资源浪费,因为客户端不再侦听它们。

问题:

  1. 我将如何丢弃这 96 个请求,即如果每个请求在队列中等待超过 5 秒,则丢弃它们?

python-3.x gunicorn fastapi uvicorn

7
推荐指数
0
解决办法
1464
查看次数

运行时在 FastAPI 中重新加载路由

我有一个 FastAPI 应用程序,其中基于数据库配置动态生成路由。

但是,一旦定义了路由并且应用程序运行,如果配置发生更改,似乎无法重新加载配置以便路由可以反映配置。我现在唯一的解决方案是通过重新启动 uvicorn 来手动重新启动 asgi 应用程序。

有没有什么方法可以在不停止应用程序的情况下完全重新生成路由,最好可以从 URL 调用?

python fastapi uvicorn

7
推荐指数
1
解决办法
2674
查看次数

FastAPI `run_in_threadpool` 卡住了

我已经使用异步实现了所有路线。并遵循 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 中的一些错误吗?不太确定!

python-asyncio starlette fastapi uvicorn python-anyio

7
推荐指数
1
解决办法
1万
查看次数

FastAPI 如何修复错误行走文件系统:OSError [Errno 40] 符号链接级别过多:'/sys/class/vtconsole/vtcon0/subsystem?

我有 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 中使用它。

python docker fastapi uvicorn

6
推荐指数
1
解决办法
3428
查看次数