uvloop README

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

.. image:: https://img.shields.io/github/actions/workflow/status/MagicStack/uvloop/tests.yml?branch=master :target: https://github.com/MagicStack/uvloop/actions/workflows/tests.yml?query=branch%3Amaster

.. image:: https://img.shields.io/pypi/v/uvloop.svg :target: https://pypi.python.org/pypi/uvloop

.. image:: https://pepy.tech/badge/uvloop :target: https://pepy.tech/project/uvloop :alt: PyPI - Downloads

uvloop is a fast, drop-in replacement of the built-in asyncio event loop. uvloop is implemented in Cython and uses libuv under the hood.

The project documentation can be found here <http://uvloop.readthedocs.org/>. Please also check out the wiki <https://github.com/MagicStack/uvloop/wiki>.

Performance

uvloop makes asyncio 2-4x faster.

.. image:: https://raw.githubusercontent.com/MagicStack/uvloop/master/performance.png :target: http://magic.io/blog/uvloop-blazing-fast-python-networking/

The above chart shows the performance of an echo server with different message sizes. The sockets benchmark uses loop.sock_recv() and loop.sock_sendall() methods; the streams benchmark uses asyncio high-level streams, created by the asyncio.start_server() function; and the protocol benchmark uses loop.create_server() with a simple echo protocol. Read more about uvloop in a blog post <http://magic.io/blog/uvloop-blazing-fast-python-networking/>_ about it.

Installation

uvloop requires Python 3.8 or greater and is available on PyPI. Use pip to install it::

$ pip install uvloop

Note that it is highly recommended to upgrade pip before installing uvloop with::

$ pip install -U pip

Using uvloop

As of uvloop 0.18, the preferred way of using it is via the uvloop.run() helper function:

.. code:: python

import uvloop

async def main():
    # Main entry-point.
    ...

uvloop.run(main())

uvloop.run() works by simply configuring asyncio.run() to use uvloop, passing all of the arguments to it, such as debug, e.g. uvloop.run(main(), debug=True).

With Python 3.11 and earlier the following alternative snippet can be used:

.. code:: python

import asyncio
import sys

import uvloop

async def main():
    # Main entry-point.
    ...

if sys.version_info >= (3, 11):
    with asyncio.Runner(loop_factory=uvloop.new_event_loop) as runner:
        runner.run(main())
else:
    uvloop.install()
    asyncio.run(main())

Building From Source

To build uvloop, you'll need Python 3.8 or greater:

  1. Clone the repository:

    .. code::

    $ git clone --recursive [email protected]:MagicStack/uvloop.git $ cd uvloop

  2. Create a virtual environment and activate it:

    .. code::

    $ python3 -m venv uvloop-dev $ source uvloop-dev/bin/activate

  3. Install development dependencies:

    .. code::

    $ pip install -e .[dev]

  4. Build and run tests:

    .. code::

    $ make $ make test

License

uvloop is dual-licensed under MIT and Apache 2.0 licenses.

uvloop (MagicStack/uvloop)

Searching Database…

11.9K ·GitHub
这是什么?uvloop 是 Python 内置 asyncio 事件循环的一个快速替代品,可直接替换使用,它基于 libuv 库构建。
这是我要导入的库,还是独立应用?它是一个 Python/Cython 库,用 pip 安装后,就可以作为 asyncio 的事件循环实现接入;没有命令行工具,也没有单独的应用程序。
我该怎么把代码切换到 uvloop?在 3.11 之前的 Python 中,先调用 uvloop.install(),然后照常运行 asyncio.run(main());在 3.11 及以上版本中,使用 asyncio.Runner(loop_factory=uvloop.new_event_loop),并调用 runner.run(main())。
我需要自己编译吗,还是直接 pip install 就行?PyPI 上提供了预编译的 wheel 包,日常使用直接安装即可;只有当你想参与开发 uvloop 本身时,才需要从源码构建(需要 Python 3.8+、make、C 编译器,以及用于拉取内置 libuv 子模块的递归 git 克隆)。
uvloop 能在 Windows 上用吗?不能。uvloop 只支持 Linux 和 macOS,不支持 Windows,因为它封装 libuv 的方式依赖于类 POSIX 的 asyncio 内部机制。
libuv 是什么?uvloop 为什么要用它?libuv 是一个高性能的 C 事件循环库,也是 Node.js 所采用的同款库;uvloop 用 Cython 对它进行封装,让 Python 的 asyncio 获得 libuv 的速度,同时你编写异步代码的方式保持不变。
我之前为 asyncio 写的代码能配合 uvloop 使用吗?可以。它的设计就是即插即用的替代品,只要把 uvloop 安装为事件循环,已有的基于 asyncio 的应用无需改动即可继续运行。
要用 uvloop,我需要重写 async/await 代码吗?不需要,无需重写任何东西。你只需把 uvloop 安装为事件循环实现,现有的 async/await 代码就能在它之上运行。
What are similar projects?discord.py, vert.x, aiohttp, libevent, swoole-src, gnet, tokio, pingora
Creator
MagicStack
Size
1.8 MB
Stars
11.9K
Age
10y
Commits · 90 Days