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

Python full stack series 146 aiomysql asynchronous read test

編輯:Python

explain

Years ago , There is a production project . You need to send queries to multiple data tables when each customer application arrives , And in 1 The analysis results will be given in minutes . Just started using synchronization , In the case of a small amount of data, it also took dozens of seconds to read . After using asynchrony, the time is shortened , Almost ignored .

I don't think I will use asynchronous reading in the future mysql 了 , I didn't expect to review the familiar formula over the years .

Content

I have written some before Asynchronous network request The article .

in general , The two methods are quite similar .asyncio + aiohttp or asyncio + aiomysql. The development mode is bottom-up :

  • 1 Construct specific asynchronous query functions
  • 2 The asynchronous query function uses some parameter offsets ( For example, put 1 individual 1 Ten thousand tasks are divided into 100 individual 100 Task of article ), Add asynchronous task list
  • 3 Get the... Of the current process loop, Start distributing asynchronous tasks and executing

1 Why asynchrony is faster ?

In essence, it is the rational use of resources . If CPU Or bandwidth , Any bottleneck has reached , Then asynchronous will not be faster ( Probably a little slower , Because there are expenses ).

adopt aio The way of asynchrony can be called IO asynchronous .

Because there are delays in information exchange on the network , The synchronization method always waits until one thing is finished before doing the next , So if there is A lot of fragmentation Task request for , that CPU Waiting ( Free ) The state of is very long .

Asynchronous mode is to synchronously initiate multiple task requests , There is no need for blocking


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