尝试在 App Engine 上托管 API 时,不断出现以下错误。该程序曾经在 Flask 上运行,Flask 可以运行,但速度非常慢。
"Traceback (most recent call last):
File "/env/lib/python3.7/site-packages/gunicorn/workers/sync.py", line 134, in handle
self.handle_request(listener, req, client, addr)
File "/env/lib/python3.7/site-packages/gunicorn/workers/sync.py", line 175, in handle_request
respiter = self.wsgi(environ, resp.start_response)
TypeError: __call__() missing 1 required positional argument: 'send'
"
Run Code Online (Sandbox Code Playgroud)
FROM gcr.io/google_appengine/python
RUN apt-get update && apt-get install -y ffmpeg
# Create a virtualenv for dependencies. This isolates these packages from
# system-level packages.
RUN virtualenv /env -p python3.7
# Setting these environment variables are …Run Code Online (Sandbox Code Playgroud) python google-app-engine gunicorn google-cloud-platform fastapi
我已经阅读了 FastAPI 关于中间件的文档(特别是中间件教程、CORS 中间件部分和高级中间件指南),但找不到如何编写可以使用该add_middleware函数添加的中间件类的具体示例(相反到使用装饰器添加的基本中间件功能),也不在此站点上。
我更喜欢使用add_middleware基于应用程序的装饰器的原因是,我想在共享库中编写一个中间件,该库将由多个不同的项目使用,因此我无法将其绑定到特定实例FastAPI。
所以我的问题是:你是如何做到的?
晚上好,
\n我正在使用 python 3.9 并尝试根据互联网上的文档在 Windows 10 Pro 上运行新的 FastAPI 服务https://www.uvicorn.org/我执行了以下语句
\npip install uvicorn pip install uvicorn[standard]\nRun Code Online (Sandbox Code Playgroud)\n创建示例文件 app.py
\nfrom fastapi import FastAPI\n\napp = FastAPI()\n\n\n@app.get("/")\nasync def root():\n return {"message": "Hello World"}\nRun Code Online (Sandbox Code Playgroud)\n但是当我运行下面的代码时:
\nuvicorn main:app --reload\n\n\nuvicorn : The term \'uvicorn\' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify t\nhat the path is correct …Run Code Online (Sandbox Code Playgroud) 我们正在使用 Python FastAPI 编写一个 Web 服务,该服务将托管在 Kubernetes 中。出于审计目的,我们需要保存特定路由的request/的原始 JSON 正文。JSON的主体大小约为1MB,最好这不应该影响响应时间。我们怎样才能做到这一点?responserequestresponse
我有一个用 FastAPI 编写的简单路线,如下所示,
from fastapi import FastAPI
app = FastAPI()
@app.get("/foo/bar/{rand_int}/foo-bar/")
async def main(rand_int: int):
return {"path": f"https://some-domain.com/foo/bar/{rand_int}/foo-bar/?somethig=foo"}
Run Code Online (Sandbox Code Playgroud)
我怎样才能“以编程方式”获取当前路径,
some-domain.com)/foo/bar/{rand_int}/foo-bar/)?somethig=foo)我在我的应用程序中遇到此错误,但我不知道为什么。经过多次搜索和调试后发现,当我在获得响应之前刷新请求时(取消请求并在处理上一个请求时发送另一个请求),就会发生这种情况。由于我的应用程序需要超过 2 秒的时间来响应,因此我遇到了太多此类错误。
到目前为止,我从我的中间件中知道了它,但我不知道为什么会发生这种情况以及我应该做什么。
知道如何解决这个问题吗?
这是我得到的错误:
ERROR: Exception in ASGI application
Traceback (most recent call last):
File "/usr/local/lib/python3.9/site-packages/anyio/streams/memory.py", line 81, in receive
return self.receive_nowait()
File "/usr/local/lib/python3.9/site-packages/anyio/streams/memory.py", line 76, in receive_nowait
raise WouldBlock
anyio.WouldBlock
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.9/site-packages/starlette/middleware/base.py", line 35, in call_next
message = await recv_stream.receive()
File "/usr/local/lib/python3.9/site-packages/anyio/streams/memory.py", line 101, in receive
raise EndOfStream
anyio.EndOfStream
During handling of the above exception, another exception occurred:
Traceback (most recent call last): …Run Code Online (Sandbox Code Playgroud) 我一直在修改 Flask 和 FastAPI 以了解它如何充当服务器。
我想知道的主要事情之一是 Flask 和 FastAPI 如何处理来自多个客户端的多个请求。
特别是当代码有效率问题时(数据库查询时间长)。
所以,我尝试制作一个简单的代码来理解这个问题。
代码很简单,当客户端访问路由时,应用程序休眠10秒才返回结果。
它看起来像这样:
快速API
import uvicorn
from fastapi import FastAPI
from time import sleep
app = FastAPI()
@app.get('/')
async def root():
print('Sleeping for 10')
sleep(10)
print('Awake')
return {'message': 'hello'}
if __name__ == "__main__":
uvicorn.run(app, host="127.0.0.1", port=8000)
Run Code Online (Sandbox Code Playgroud)
烧瓶
from flask import Flask
from flask_restful import Resource, Api
from time import sleep
app = Flask(__name__)
api = Api(app)
class Root(Resource):
def get(self):
print('Sleeping for 10')
sleep(10)
print('Awake')
return {'message': 'hello'}
api.add_resource(Root, …Run Code Online (Sandbox Code Playgroud) 我用一些参数做了一个帖子,但其中一个返回“field required”和“value_error.missing”,但该字段在那里并且有一个值。查看 Postman 的输出。

在 schemas.py 中,字段定义如下:
class Message(BaseModel):
title: str
id: int
datim: Optional[datetime]
to_id: Optional[int]
from_id: Optional[int]
body: Optional[str]
class Config:
orm_mode = True
Run Code Online (Sandbox Code Playgroud)
那么它为什么要抱怨“身体”呢?
我正在尝试解决以下错误:
\nasyncpg.exceptions._base.InterfaceError: cannot perform operation: another operation is in progress\nRun Code Online (Sandbox Code Playgroud)\n这是完整的回溯:
\nTraceback (most recent call last):\n\n File "<string>", line 1, in <module>\n File "/Users/ddd/.pyenv/versions/3.8.6/lib/python3.8/multiprocessing/spawn.py", line 116, in spawn_main\n exitcode = _main(fd, parent_sentinel)\n \xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x94 4\n \xe2\x94\x82 \xe2\x94\x94 7\n \xe2\x94\x94 <function _main at 0x109c8aca0>\n File "/Users/ddd/.pyenv/versions/3.8.6/lib/python3.8/multiprocessing/spawn.py", line 129, in _main\n return self._bootstrap(parent_sentinel)\n \xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x94 4\n \xe2\x94\x82 \xe2\x94\x94 <function BaseProcess._bootstrap at 0x109b1f8b0>\n \xe2\x94\x94 <SpawnProcess name='SpawnProcess-4' parent=36604 started>\n File "/Users/ddd/.pyenv/versions/3.8.6/lib/python3.8/multiprocessing/process.py", line 315, in _bootstrap\n self.run()\n \xe2\x94\x82 \xe2\x94\x94 <function …Run Code Online (Sandbox Code Playgroud) fastapi ×10
python ×8
python-3.x ×2
asgi ×1
asynchronous ×1
asyncpg ×1
favicon ×1
flask ×1
gunicorn ×1
logging ×1
middleware ×1
pydantic ×1
starlette ×1
uvicorn ×1
wsgi ×1