uvloop--Super fast Python Asynchronous Network framework

Source: Internet
Author: User
Tags benchmark

Brief introduction

Asyncio is an asynchronous I/O framework that follows the Python standard library. In this article, I'll introduce uvloop: a complete alternative to the Asyncio event loop. Uvloop is written in Cython, based on LIBUV.

Uvloop makes Asyncio faster. In fact, it's at least twice times faster than nodejs,gevent, and any other Python async framework. Uvloop Asyncio Performance-based testing is close to go programs.

Asyncio and Uvloop

The Asyncio module, introduced in PEP 3156, is a collection that contains network transports, protocols, and abstract streams with pluggable event loops. The event loop is the core of Asyncio. It provides the following APIs:

1th paragraph (1.41 points can be obtained)Empower 1 years ago
    • Scheduling of calling methods
    • Transferring data over the network
    • Perform DNS queries,
    • Handling OS operating system signals
    • Encapsulating the creation of servers and connections
    • Child process Asynchronous processing

Currently Uvloop only supports *nix platform and Python 3.5.

Uvloop is a replacement for Python's built-in Asyncio event loop, which you can install via PIP:

$ pip Install Uvloop

Using Uvloop in your Asyncio code is very simple:

Import asyncioimport uvloopasyncio.set_event_loop_policy (uvloop.  Eventlooppolicy ())

The code snippet above lets you asyncio.get_event_loop() return an instance of Uvloop.

2nd paragraph (0.96 points can be obtained)CY2 2 years ago

You can also explicitly create an Uvloop instance by invoking theuvloop.new_event_loop()。

System structure

Uvloop is written in Cython, and is built on top of LIBUV.

LIBUV is a high-performance, cross-platform asynchronous I/O class library that Nodejs also uses. Since Nodejs is so widespread and popular, it can be known that LIBUV is fast and stable.

Uvloop implements all the Asyncio event loop APIs. The high-level Python object wraps the lower levels of the LIBUV struct and function methods. Inheritance can keep your code dry (do not repeat yourself) and ensure that any manual memory management can be synchronized with the life cycle of LIBUV's native type.

3rd paragraph (1.09 points can be obtained)Banna 9 months agoBenchmark Test

In order to detect Uvloop stack performance compared to other implementations, we created the Toolbench benchmark for standard TCP and UNIX socket I/O, and HTTP protocol performance benchmarks.

The benchmark server runs inside a docker container that contains an external load generation tool (WRK HTTP benchmark), which tests for request throughput and latency.

All the benchmarks in this blog are running on the Intel Xeon CPU e5-1620 v2 @ 3.70GHz Ubuntu Linux system. We are using Python 3.5, and all servers are single-core. In addition, the go code is used GOMAXPROCS=1 , Nodejs does not use a cluster, and all Python servers are single-threaded. The Tcp_nodelay identity is set for each benchmark set.

4th paragraph (1.33 points can be obtained)Empower 1 years ago

The benchmark report results on Mac OS X are also very similar.

Tcp

This benchmark tests the performance of a simple echo server using a different number of messages. We used 1, 10, and KiB packages respectively. The concurrency level is 10. Each benchmark runs for 30 seconds.

You can click here to view the full TCP benchmark report.

Some comments for each location:

  1. asyncio-streams. The event loop implemented by Asyncio and its built-in pure python. In this benchmark, we tested the abstraction performance of high-level streams. We use asyncio.create_server()来创建一个服务器,把一对 (reader, writer) 传递给 client-side collaboration programs.
  2. Tornado. This server implements a very simple tornado protocol that responds to incoming messages immediately.
  3. curio-streams. Curio is a new member on the Python Aio library. Similar to asyncio-streams , in this benchmark we tested the curio stream, using curio.make_streams()来创建了一对 (reader, writer),它提供了一些 advanced APIs such asreadline()。
  4. Twisted. Similar to tornado, we tested a minimal echo protocol.
  5. Curio. This benchmark tests the performance of Curio sockets: This is a sock.recv() collaborative program that implements and sock.sendall() tightly loops.
  6. uvloop-streams. As mentioned in # #, here we test the performance of the Asyncio premium stream, but this is now based on Uvloop.
  7. gevent. The receiving data is sent in a tight loop by using the gevent.StreamServer和一个 gevent socket.
  8. Asyncio. It looks like the average asyncio is very fast! Similar to the 2nd and 4 points, we tested a minimal echo protocol, which was implemented using the pure Python Asyncio.
  9. Nodejs. We use the net.createServer API to test the performance of the stream in Nodejs v4.2.6.
  10. Uvloop. This benchmark tests a minimal echo protocol (like #2, #4, #8), which is implemented using Uvloop-based Asyncio. With 1 KiB messages, Uvloop is the fastest implementation and can be as high as 105,000 requests per second! With the KiB message, the Uvloop speed can reach about 2.3 gib/s.
  11. Go. A net.Conn.Read/Write close loop of a call. The Golang performance is very close to Uvloop and is slightly better in the case of 10 and KiB messages.
5th paragraph (3.96 points can be obtained)Banna 9 months ago

How to read the box diagram: (Here is a picture, the original is missing, not brought here)

The code for all benchmark tests can be found here.

You can also view all the UNIX socket benchmark results.

HTTP

Initially, we wanted to test for Nodejs and go on Asyncio and Uvloop. Aiohttp is the most popular framework for writing asynchronous HTTP servers and clients using Asyncio.

You can also view the full HTTP benchmark report.

However, the performance bottleneck on the aiohttp is indeed its HTTP parser, which is slower, so even the use of the I/O class library is no longer very fast. To make things more interesting, we created a python binding (binding) for Http-parser (Nodejs's HTTP parser Class C library, originally developed for Nginx). This class library is named Httptools and can be found on both GitHub and PyPI.

6th paragraph (1.54 points can be obtained)Banna 9 months ago

For HTTP, all baselines use WRK to generate the load. The concurrency level is set to 300. The duration of each benchmark is 30 seconds.

Surprisingly, the pure Python implementation of the Asyncio with the help of a high-performance HTTP parser is much faster than nodejs using the same HTTP parser!

Go is quicker in response to 1 KiB, but the combination of Uvloop and Asyncio is much faster in 10/100 KiB response. The service quality of Asyncio and Uvloop using Httptools is great, and it's the same for go.

Admittedly, Httptools-based servers are very small and do not contain any routing logic like other implementations. Nonetheless, this benchmark demonstrates how quickly uvloop and an efficient protocol can be combined.

7th paragraph (1.39 points can be obtained)Banna 9 months agoConclusion

We can conclude that using Uvloop can write Python network code that can make tens of thousands of requests per second in a single CPU core. Under multi-core systems, you can use process pooling to further improve system performance.

Uvloop and Asyncio, coupled with the power of async/await in Python 3.5, make it easier to write high-performance network code using Python.

Try the next Uvloop (GitHub) and share the results!

uvloop--Super fast Python Asynchronous Network framework

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.