by bossjones
此技能提供使用 Uvicorn 运行 Python ASGI 应用程序的文档和示例。涵盖带热重载的开发服务器、带工作进程的生产部署、SSL 配置以及 Docker 集成。
1. 打开 Claude 聊天界面
2. 点击下方 "📋 复制" 按钮
3. 粘贴到 Claude 聊天框中并发送
4. 输入 "使用 uvicorn 技能" 开始使用
=== uvicorn 技能 === 作者: bossjones 描述: 此技能提供使用 Uvicorn 运行 Python ASGI 应用程序的文档和示例。涵盖带热重载的开发服务器、带工作进程的生产部署、SSL 配置以及 Docker 集成。 使用方法: 1. 调用技能: "使用 uvicorn 技能" 2. 提供相关信息: 根据技能要求提供必要参数 3. 查看结果: 技能会返回处理结果 示例: "使用 uvicorn 技能,帮我分析一下这段代码"
这种方法适用于所有 Claude 用户,不需要安装额外工具。
devops
safe
name: uvicorn description: ASGI server for Python web applications - Fast, production-ready server for async frameworks when_to_use: |
Uvicorn is a lightning-fast ASGI server implementation, using uvloop and httptools. It's the go-to server for modern Python async web frameworks.
# Run ASGI app
uv run uvicorn main:app
# With host/port
uv run uvicorn main:app --host 0.0.0.0 --port 8000
# Development with auto-reload
uv run uvicorn main:app --reload
# main.py
async def app(scope, receive, send):
assert scope['type'] == 'http'
await send({
'type': 'http.response.start',
'status': 200,
'headers': [(b'content-type', b'text/plain')],
})
await send({
'type': 'http.response.body',
'body': b'Hello, World!',
})
# main.py
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
async def root():
return {"message": "Hello World"}
uv run uvicorn main:app --reload
# main.py
from fastapi import FastAPI
def create_app():
app = FastAPI()
# Configure app
return app
app = create_app()
uv run uvicorn --factory main:create_app
import uvicorn
# Simple run
if __name__ == "__main__":
uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True)
import asyncio
import uvicorn
async def main():
config = uvicorn.Config("main:app", port=5000, log_level="info")
server = uvicorn.Server(config)
await server.serve()
if __name__ == "__main__":
asyncio.run(main())
export UVICORN_HOST="0.0.0.0"
export UVICORN_PORT="8000"
export UVICORN_RELOAD="true"
uv run uvicorn main:app
# Use multiple worker processes
uv run uvicorn main:app --workers 4
# Note: Can't use --reload with --workers
# Install gunicorn
uv add gunicorn
# Run with Gunicorn process manager
uv run gunicorn main:app -w 4 -k uvicorn.workers.UvicornWorker
uv run uvicorn main:app --ssl-keyfile=./key.pem --ssl-certfile=./cert.pem
uv run uvicorn main:app --uds /tmp/uvicorn.sock
uv run uvicorn main:app \
--host 0.0.0.0 \
--port 8000 \
--reload \
--reload-dir ./app \
--log-level info \
--access-log \
--workers 4
--host: Bind host (default: 127.0.0.1)--port: Bind port (default: 8000)--reload: Enable auto-reload for development--workers: Number of worker processes--log-level: Logging level (critical, error, warning, info, debug)--access-log: Enable access logging--factory: Treat app as application factoryFROM python:3.12-slim
WORKDIR /app
# Install dependencies
COPY requirements.txt .
RUN pip install -r requirements.txt
# Copy application
COPY . .
# Run with uvicorn
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
services:
app:
build: .
ports:
- "8000:8000"
environment:
- UVICORN_RELOAD=true
volumes:
- .:/app
command: uvicorn main:app --host 0.0.0.0 --port 8000 --reload
--app-dir--reload-dir--workers for production, avoid with --reloaduv run uvicorn main:app --reload --log-level debug
@app.get("/health")
async def health():
return {"status": "healthy"}
View Count
0
Download Count
0
Favorite Count
0
Quality Score
70