程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

You must know uvloop! Make Python asyncios asynchronous programming performance close to that of go coroutine

編輯:Python

Keep creating , Accelerate growth ! This is my participation 「 Nuggets day new plan · 6 Yuegengwen challenge 」 Of the 19 God , Click to see the event details .

background

I developed 《 Online Board Games Collection 》 when , Using the WebSocket agreement , I use Python Realized , I use the Python A library :daphne.daphne Is based on python Native asyncio The library of .

Recently, I was thinking about back-end optimization , I learned that uvloop, It is python Native asyncio Alternative to event cycle .

First introduce asyncio

Python from 3.4 Start , Introduced asyncio library , Reference resources PEP-3156.Python from 3.5 Start , Introduced async and await grammar , Reference resources PEP-0492.

Python In fact, the usage of javascript Medium async and await be similar , After all, the specification of scripting language , Basically, they copy around , Are willing to absorb the excellent grammar of the language next door . This is also great for developers , It's easier to learn another language .

Python3.4 and 3.5 It was a revolution , Since then ,Python Asynchronous programming is becoming more and more popular ( Based on the coroutine ).

Maybe you don't understand the process , Let me give you a brief introduction :

We used to use Python Write about reptiles , May use requests This library ,requests.get(url) Send the request , When the result comes , The code continues . It is the execution of sequential synchronization . When a request occurs , You can only wait foolishly , You can't let code do another thing . Unless you start a new thread to do another thing .

But now , We have asynchronous programming . be based on Built in event loop , We can achieve this effect : encounter I/O operation ( for example Http request ) when , Let's pause this code first ( This task ) Implementation , Go and see if there are any other tasks you can perform , If there are other tasks to perform , Just perform other tasks . If all tasks cannot be performed , Just wait , Until there is 1 Tasks are available , Just do it , Until it runs out or encounters I/O operation , Pause it again , Perform other tasks .

Be careful ,Python Asynchronous programming in , It's based on the event loop . today , What we want to talk about is the event cycle .

About uvloop

uvloop yes Python Native event loop substitute . Improved performance .

Why improved performance

( The saying comes from uvloop The official blog )

  1. uvloop Is based on Cython Written , More efficient execution .
  2. uvloop Bottom based libuv, and libuv It's high performance 、 Cross platform asynchrony IO library ,nodejs The event loop of is also based on libuv Of .

Benchmarks

I didn't do it , I just forward it uvloop Official test results .

There are two tests :TCP The protocol and HTTP agreement .

TCP

This is in a simple echo Service testing , Not representative of all production environments .

Use them separately 1KiB Data packets 、10KiB、100KiB Tested .

You can see uvloop It's really awesome , Performance is straight ahead Go.

HTTP

Explain : Officials had intended to use aiohttp This Python The most commonly used asynchronous in http Service library test . But it's http The parser is too inefficient , It has become the bottleneck of the whole system , So they did it manually http-parser( At first it was for Nginx The design of the , So it's very efficient ) Of Python edition , be called httptools.

You can see ,asyncio Native event loops and uvloop, Yes aiohttp No performance improvement !( After the play , I think : I dare not use it in the future aiohttp 了 )

But the http After the bottleneck of the parser is solved ,asyncio Native event loops and uvloop The performance has been significantly improved , but uvloop Obviously more awesome , stay 10KiB and 100KiB scenario , Performance goes beyond Go.

uvloop Do you have any shortcomings

Yes , I won't support it Windows.

How do you use it? uvloop

install

pip install uvloop

introduce

import asyncio
import uvloop
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())

Other async The code remains the same , You enjoy a faster cycle of events !

feel

If you blindly pursue performance , Or directly Go Well .

however uvloop Do give Python Developers provide more performance , about Python Developers are happy . But also pay attention to , Are there any other performance bottlenecks , For example, with aiohttp When used in combination , The bottleneck may lie in http analysis .

Reference blog :uvloop: Blazing fast Python networking

At the end

I am a HullQin, Independently developed 《 Online Board Games Collection 》, It's a web page , It is very convenient to play landlords online with friends 、 gobang 、UNO Such games , No charge , No advertising , Click here to start playing . Also developed independently 《 Synthetic watermelon reprocessing 》. You can pay attention to me if you like ~ When I am free, I will share the related technology of playing games , It will be here 2 Share in a column :《 Teach you to play small games 》、《 Ultimate user experience 》.


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved