Skip to content

MagicStack/uvloop

Repository files navigation

https://img.shields.io/github/actions/workflow/status/MagicStack/uvloop/tests.yml?branch=master 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.Please also check out the wiki.

Performance

uvloop makes asyncio 2-4x faster.

https://raw.githubusercontent.com/MagicStack/uvloop/master/performance.png

The above chart shows the performance of an echo server with different message sizes. Thesocketsbenchmark usesloop.sock_recv()and loop.sock_sendall()methods; thestreamsbenchmark uses asyncio high-level streams, created by theasyncio.start_server()function; and theprotocolbenchmark usesloop.create_server()with a simple echo protocol. Read more about uvloop in a blog post 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 toupgrade pip beforeinstalling 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:

importuvloop

asyncdefmain():
# Main entry-point.
...

uvloop.run(main())

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

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

importasyncio
importsys

importuvloop

asyncdefmain():
# Main entry-point.
...

ifsys.version_info>=(3,11):
withasyncio.Runner(loop_factory=uvloop.new_event_loop)asrunner:
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:

    $ git clone --recursive git@github:MagicStack/uvloop.git
    $ cd uvloop
    
  2. Create a virtual environment and activate it:

    $ Python 3.7 -m venv uvloop-dev
    $ source uvloop-dev/bin/activate
    
  3. Install development dependencies:

    $ pip install -e.[dev]
    
  4. Build and run tests:

    $ make
    $ make test
    

License

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