uvicorn README

The project's README from github.com/Kludex/uvicorn. As one markdown file: README.md.

uvicorn

An ASGI web server, for Python.


Build Status Package version Supported Python Version Discord


Documentation: https://uvicorn.dev

Source Code: https://www.github.com/Kludex/uvicorn


Uvicorn is an ASGI web server implementation for Python.

Until recently Python has lacked a minimal low-level server/application interface for async frameworks. The ASGI specification fills this gap, and means we're now able to start building a common set of tooling usable across all async frameworks.

Uvicorn currently supports HTTP/1.1, HTTP/2 and WebSockets.

Quickstart

Install using pip:

$ pip install uvicorn

This will install uvicorn with minimal (pure Python) dependencies.

$ pip install 'uvicorn[standard]'

This will install uvicorn with "Cython-based" dependencies (where possible) and other "optional extras".

In this context, "Cython-based" means the following:

Moreover, "optional extras" means that:

Create an application, in example.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!',
    })

Run the server:

$ uvicorn example:app

Why ASGI?

Most well established Python Web frameworks started out as WSGI-based frameworks.

WSGI applications are a single, synchronous callable that takes a request and returns a response. This doesn’t allow for long-lived connections, like you get with long-poll HTTP or WebSocket connections, which WSGI doesn't support well.

Having an async concurrency model also allows for options such as lightweight background tasks, and can be less of a limiting factor for endpoints that have long periods being blocked on network I/O such as dealing with slow HTTP requests.


Alternative ASGI servers

A strength of the ASGI protocol is that it decouples the server implementation from the application framework. This allows for an ecosystem of interoperating webservers and application frameworks.

Daphne

The first ASGI server implementation, originally developed to power Django Channels, is the Daphne webserver.

It is run widely in production, and supports HTTP/1.1, HTTP/2, and WebSockets.

Any of the example applications given here can equally well be run using daphne instead.

$ pip install daphne
$ daphne app:App

Hypercorn

Hypercorn was initially part of the Quart web framework, before being separated out into a standalone ASGI server.

Hypercorn supports HTTP/1.1, HTTP/2, and WebSockets.

It also supports the excellent trio async framework, as an alternative to asyncio.

$ pip install hypercorn
$ hypercorn app:App

Mangum

Mangum is an adapter for using ASGI applications with AWS Lambda & API Gateway.

Granian

Granian is an ASGI compatible Rust HTTP server which supports HTTP/2, TLS and WebSockets.


Uvicorn is BSD licensed code.
Designed & crafted with care.

— 🦄 —

uvicorn (Kludex/uvicorn)

Searching Database…

这是干什么的?Uvicorn 是一个面向 Python 的 ASGI Web 服务器——一种异步服务器,用于运行 Python Web 应用和 API,支持 HTTP/1.1、HTTP/2 和 WebSocket。
ASGI 服务器和经典 Python 框架背后用的服务器有什么不同?经典 WSGI 应用把每个请求当作一次同步调用来处理,所以 WebSocket 这类长连接或长轮询 HTTP 并不太适用。ASGI 是异步的——它支持这些场景,还能运行轻量的后台任务。
怎么把我的应用跑在它上面?先用 pip 安装,然后在一个类似 example.py 的文件里定义一个 ASGI 应用,最后在终端里运行 uvicorn example:app。
我应该直接 pip install uvicorn,还是用 [standard] 这个额外选项?基础安装是纯 Python 的,依赖很少。uvicorn[standard] 会额外加入基于 Cython 的提速组件——uvloop 事件循环和处理 HTTP 的 httptools——还有一些附加组件:websockets、用于 --reload 的 watchfiles、python-dotenv 和 PyYAML。
它在 Windows 和 macOS 上能跑吗,还是只能用 Linux?它就是一个普通的 pip 包,对操作系统没有要求,所以 Python 能跑的地方它都能跑——Linux 服务器、macOS 和 Windows 开发机都没问题。
Uvicorn 和 Daphne、Hypercorn 比怎么样?它们更像是可以互相替换的兄弟,而不是竞争对手:ASGI 让任何服务器都能运行任何应用。Daphne 是最早的 ASGI 服务器(为 Django Channels 而建),Hypercorn 增加了 trio 作为 asyncio 的替代方案,Mangum 可以把 ASGI 应用适配到 AWS Lambda 上,Granian 则是一个基于 Rust 的服务器。
为什么会有 ASGI 这个东西?Python 长期以来缺少一个异步服务器和框架之间的最小底层接口。ASGI 填补了这个空白,让一套通用的工具可以用于所有异步框架——Uvicorn 就是在它之上构建的服务器之一。
Uvicorn 经受过实战检验吗,还是仍处于实验阶段?经受过实战检验:它从 2017 年起就在 GitHub 上开发,已有超过 11000 个 star。
Creator
Kludex
Size
5.6 MB
Stars
11K
Age
9y
Commits · 90 Days