我有一个烧瓶应用程序。
我使用以下命令在生产中运行它:
python -m gunicorn -w 1 -b 0.0.0.0:5000 "path.to.wsgi:return_app()"
Run Code Online (Sandbox Code Playgroud)
相反,我想在 my_file.py 中运行它
我需要一个函数来运行,它应该接受应用程序对象和端口绑定以及工作人员数量
我怎样才能做到这一点?
我需要这样的伪代码:
import gunicorn
app = return_app()
gunicorn(workers=1, ip="0.0.0.0", port=5000, app=app)
Run Code Online (Sandbox Code Playgroud)
对我来说最重要的部分app=app
是
要点是我想使用 app 对象作为 Flask() 的实例。我想直接将应用程序对象提供给gunicorn,而不是通过在字符串中寻址它
我尝试过的: 我已经打开了gunicorn库主.py文件
from gunicorn.app.wsgiapp import run
run()
Run Code Online (Sandbox Code Playgroud)
看看它是如何工作的,但无法弄清楚
def run():
"""\
The ``gunicorn`` command line runner for launching Gunicorn with
generic WSGI applications.
"""
from gunicorn.app.wsgiapp import WSGIApplication
WSGIApplication("%(prog)s [OPTIONS] [APP_MODULE]").run()
Run Code Online (Sandbox Code Playgroud) 这是我的问题的简化版本。
我有一个 python 应用程序,其结构如下:
my_project
my_app
__init__.py
settings.py
tests
__init__.py
conftest.py
my_test.py
venv
Run Code Online (Sandbox Code Playgroud)
# settings.py
from dotenv import load_dotenv
load_dotenv()
MY_VALUE = os.environ["MY_KEY"]
Run Code Online (Sandbox Code Playgroud)
由于某些原因,我不想.env
在此处添加文件。我也不想使用 get 方法os.environ
我想运行这个测试
# my_test.py
from my_app import settings # I need this for some reasons
def test_something():
assert True
def test_env():
assert os.environ["MY_KEY"] == 'MY_VALUE'
Run Code Online (Sandbox Code Playgroud)
但是,我得到了一个KeyError
,因为一旦我 import settings.py
,该行MY_VALUE = os.environ["MY_KEY"]
就会运行,并且因为MY_KEY
不在 env 中,所以我得到了KeyError
.
我想我可以进行如下的conftest
import os
from unittest import mock
import pytest
@pytest.fixture(autouse=True)
def …
Run Code Online (Sandbox Code Playgroud) 我已经安装了 Visual Studio Code 扩展Python 文档字符串生成器,它可以自动生成文档字符串片段。
我使用的自定义设置:
"autoDocstring.docstringFormat": "sphinx",
"autoDocstring.generateDocstringOnEnter": true,
"autoDocstring.includeExtendedSummary": true,
"autoDocstring.guessTypes": true,
"autoDocstring.customTemplatePath": ".vscode/sphinx.mustache",
Run Code Online (Sandbox Code Playgroud)
然而,它对于函数的文档字符串最有用。
我怎样才能生成:
__init__.py
文件中在这方面有什么想法吗?
我需要在 gitlab ci 作业规则中重用变量
include:
- template: "Workflows/Branch-Pipelines.gitlab-ci.yml"
.staging_variables:
variables:
CONFIG_NAME: "staging"
.staging_rules:
rules:
- if: $CI_COMMIT_BRANCH == $STAGING_BRANCH
variables: !reference [.staging_variables, variables]
stages:
- staging
staging:
stage: staging
rules:
- !reference [.staging_rules, rules]
script:
- echo $CONFIG_NAME
tags:
- staging
Run Code Online (Sandbox Code Playgroud)
但是,我看到了这个Syntax is incorrect
linting 错误:
jobs:staging:rules:rule:variables config should be a hash of key value pairs
Run Code Online (Sandbox Code Playgroud)
我正在使用这里解释的示例:
https://docs.gitlab.com/ee/ci/yaml/yaml_optimization.html#reference-tags
请注意,我可以做到这一点并且有效:
include:
- template: "Workflows/Branch-Pipelines.gitlab-ci.yml"
.staging_rules:
rules:
- if: $CI_COMMIT_BRANCH == $STAGING_BRANCH
variables:
CONFIG_NAME: "staging"
stages:
- staging
staging:
stage: staging …
Run Code Online (Sandbox Code Playgroud) 我有这个应用程序:
import enum
from typing import Annotated, Literal
import uvicorn
from fastapi import FastAPI, Query, Depends
from pydantic import BaseModel
app = FastAPI()
class MyEnum(enum.Enum):
ab = "ab"
cd = "cd"
class MyInput(BaseModel):
q: Annotated[MyEnum, Query(...)]
@app.get("/")
def test(inp: MyInput = Depends()):
return "Hello world"
def main():
uvicorn.run("run:app", host="0.0.0.0", reload=True, port=8001)
if __name__ == "__main__":
main()
Run Code Online (Sandbox Code Playgroud)
curl http://127.0.0.1:8001/?q=ab
或curl http://127.0.0.1:8001/?q=cd
返回“Hello World”
但任何这些
curl http://127.0.0.1:8001/?q=aB
curl http://127.0.0.1:8001/?q=AB
curl http://127.0.0.1:8001/?q=Cd
返回422Unprocessable Entity
这是有道理的。
如何使此验证不区分大小写?
我愿意
git clone https://github.com/openzipkin/zipkin.git
zipkin
创建一个 Dockerfile 如下
FROM openjdk
RUN mkdir app
WORKDIR /app
COPY ./ .
ENTRYPOINT ["sleep", "1000000"]
Run Code Online (Sandbox Code Playgroud)
然后
docker build -t abc .
docker run abc
然后我跑docker exec -it CONTAINER_ID bash
pwd
/app
预期回报
但我ls
看到文件没有被复制
仅将目录和 xml 文件复制到该/app
目录中
是什么原因?如何修复它?
我也尝试过
FROM openjdk
RUN mkdir app
WORKDIR /app
COPY . /app
ENTRYPOINT ["sleep", "1000000"]
Run Code Online (Sandbox Code Playgroud) 我试图有一个像这样的端点/services?status=New
status
将是 New
或者Old
这是我的代码:
from fastapi import APIRouter, Depends
from pydantic import BaseModel
from enum import Enum
router = APIRouter()
class ServiceStatusEnum(str, Enum):
new = "New"
old = "Old"
class ServiceStatusQueryParam(BaseModel):
status: ServiceStatusEnum
@router.get("/services")
def get_services(
status: ServiceStatusQueryParam = Query(..., title="Services", description="my desc"),
):
pass #my code for handling this route.....
Run Code Online (Sandbox Code Playgroud)
结果是我收到一个似乎与此问题相关的错误
错误说AssertionError: Param: status can only be a request body, using Body()
然后我找到了这里解释的另一个解决方案。
所以,我的代码将是这样的:
from fastapi import APIRouter, Depends
from pydantic …
Run Code Online (Sandbox Code Playgroud) 我有一个简单的应用程序如下:
from typing import Annotated
import uvicorn
from fastapi import FastAPI, Query, Depends
from pydantic import BaseModel
app = FastAPI()
class Input(BaseModel):
a: Annotated[str, Query(..., alias="your_name")]
@app.get("/")
def test(inp: Annotated[Input, Depends()]):
return f"Hello {inp.a}"
def main():
uvicorn.run("run:app", host="0.0.0.0", reload=True, port=8001)
if __name__ == "__main__":
main()
Run Code Online (Sandbox Code Playgroud)
curl "http://127.0.0.1:8001/?your_name=amin"
返回“你好阿明”
我现在将别名从 更改your_name
为your-name
。
from typing import Annotated
import uvicorn
from fastapi import FastAPI, Query, Depends
from pydantic import BaseModel
app = FastAPI()
class Input(BaseModel):
a: Annotated[str, Query(..., alias="your-name")] …
Run Code Online (Sandbox Code Playgroud) 我有这个 api 文档是用 OpenAPI 3.0.3 编写的
openapi: 3.0.3
info:
version: '1.0'
title: 'MyTitle'
description: Specification for Bear Store
servers:
- url: https://development.example.com/v1
description: Development Server
paths:
'/v1/bears':
get:
description: Requests a lists all the bears
summary: List of bears request
responses:
'200':
description: List of Bears
content:
application/json:
schema:
type: array
items:
type: object
properties:
id:
type: string
format: uuid
name:
type: string
Run Code Online (Sandbox Code Playgroud)
我有一个 Bear 对象,它具有id
, 和name
属性。我想定义 id 字段是一个唯一字段。我如何在 OpenAPI 3 中定义它?