跳到主要内容
高性能。高效率。专业级。

Lihil

The Official website of python webframework lihil

开始使用GitHub
openai
from lihil import Lihil, Route, Stream
from openai import OpenAI
from openai.types.chat import ChatCompletionChunk as Chunk
from openai.types.chat import ChatCompletionUserMessageParam as MessageIn
gpt = Route("/gpt", deps=[OpenAI])
def message_encoder(chunk: Chunk) -> bytes:
if not chunk.choices:
return b""
return chunk.choices[0].delta.content.encode() or b""
@gpt.sub("/messages").post(encoder=message_encoder)
async def add_new_message(
client: OpenAPI, question: MessageIn, model: str
) -> Stream[Chunk]:
async for chunk in client.responses.create(
messages=[question], model=model, stream=True
):
yield chunk

50%-100%

比其它ASGI框架更快,更适合大型应用。 性能基准

100%

测试覆盖率高且严格类型检查

> 45K

RPS(每CPU线程)


Get Started in Seconds

Lihil is designed for simplicity without sacrificing power. Install with pip and start building your next Python web application.

$|
安装

几分钟内创建REST API

app.py
from lihil import Lihil, HTTPException, Struct, Route, Param, Annotated, Empty
class TodoItem(Struct):
id: int
title: str
completed: bool = False
todo = Route("/todos", deps=[TodoRepo])
@todo.get
async def get_todos(todo_repo: TodoRepo, n: Annotated[int, Param(lt=100)]):
return await todo_repo.list_todos()
@todo.post
async def create_todo(item: TodoItem, todo_repo: TodoRepo) -> Annotated[Empty, 201]:
await todo_repo.add(item)
if __name__ == "__main__":
lhl = Lihil(todo)
lhl.run(__file__)

为什么选择lihil?

一个简洁、强大的Python Web框架,专为现代应用而构建。
参数解析与验证

使用msgspec自动解析和验证来自路径、查询、请求头和请求体的数据——比Pydantic快12倍,内存效率高25倍。

强大的依赖注入

基于类型提示注入依赖。支持工厂模式、异步、作用域和单例——一切都快如闪电。

WebSocket

使用简洁、类型安全的API处理WebSocket连接。使用内置的WebSocket测试客户端轻松测试。

OpenAPI与错误文档

自动生成OpenAPI文档和问题详情。自定义异常转化为清晰的API响应。

身份验证与授权

内置JWT和OAuth2支持。认证对象类型安全且可序列化。

消息系统

内置事件系统,高效地发布和处理进程内和跨进程事件。

出色的可测试性

内置端点、路由和中间件测试客户端——无需额外设置。

内存高效

针对最小内存使用进行优化。减少GC开销,使服务在负载下更加稳定。

AI就绪

为AI而设计。内置SSE、MCP支持,远程处理器即将推出。


Join the Community

Lihil is backed by a growing community of Python developers. Get support, contribute, and help shape the future of fast Python web development.

GitHubDiscordContribute
Latest Release

Loading...

Release Notes

准备好构建令人惊叹的应用了吗?

加入不断壮大的开发者社区,使用Lihil构建快速、健壮且可扩展的Web应用程序。

立即开始