当前位置:网站首页>Reptile efficiency improvement method
Reptile efficiency improvement method
2022-04-23 18:02:00 【Round programmer】
coroutines : In function ( Special functions ) When defining , Use async modification , After function call , Internal statements do not execute immediately , It will return a process object
Task object : Task object = Advanced collaboration objects ( Further encapsulation )= Special functions , The task object must be registered in the time cycle object , Bind callback to task object : Crawler data analysis
The event loop : As a container for loading task objects , When the event loop object is started , The task object stored inside will execute asynchronously
First flask service
from flask import Flask
import time
app = Flask(__name__)
@app.route('/ Zhang San ')
def index_bobo():
time.sleep(2)
return 'hello Zhang San !'
@app.route('/ Li Si ')
def index_jay():
time.sleep(2)
return 'hello Li Si !'
@app.route('/ Wang Wu ')
def index_tom():
time.sleep(2)
return 'hello Wang Wu !'
if __name__ == '__main__':
app.run(threaded=True)
One ,aiohttp modular + Single thread multitask asynchronous coroutine
import asyncio
import aiohttp
import requests
import time
start = time.time()
async def get_page(url):
# page_text = requests.get(url=url).text
# print(page_text)
# return page_text
async with aiohttp.ClientSession() as s: # Generate a session object
async with await s.get(url=url) as response:
page_text = await response.text()
print(page_text)
return page_text
urls = [
'http://127.0.0.1:5000/ Zhang San ',
'http://127.0.0.1:5000/ Li Si ',
'http://127.0.0.1:5000/ Wang Wu ',
]
tasks = []
for url in urls:
c = get_page(url)
task = asyncio.ensure_future(c)
tasks.append(task)
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.wait(tasks))
end = time.time()
print(end-start)
Two ,aiohttp Module implements single thread + Multitask asynchronous process
import aiohttp
import asyncio
from lxml import etree
import time
start = time.time()
# Special functions : Request sending and data capture
# Be careful async with await keyword
async def get_request(url):
async with aiohttp.ClientSession() as s:
async with await s.get(url=url) as response:
page_text = await response.text()
return page_text # Return to page source code
# Callback function , Parsing data
def parse(task):
page_text = task.result()
tree = etree.HTML(page_text)
msg = "".join(tree.xpath('//text()'))
print(msg)
urls = [
'http://127.0.0.1:5000/ Zhang San ',
'http://127.0.0.1:5000/ Li Si ',
'http://127.0.0.1:5000/ Wang Wu ',
]
tasks = []
for url in urls:
c = get_request(url)
task = asyncio.ensure_future(c)
task.add_done_callback(parse) # Bind callback function !
tasks.append(task)
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.wait(tasks))
end = time.time()
print(end-start)
3、 ... and ,requests modular + Thread pool
import time
import requests
from multiprocessing.dummy import Pool
start = time.time()
urls = [
'http://127.0.0.1:5000/ Zhang San ',
'http://127.0.0.1:5000/ Li Si ',
'http://127.0.0.1:5000/ Wang Wu ',
]
def get_request(url):
page_text = requests.get(url=url).text
print(page_text)
return page_text
pool = Pool(3)
pool.map(get_request, urls)
end = time.time()
print(' Total time :', end-start)
版权声明
本文为[Round programmer]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230545315261.html
边栏推荐
- Laser slam theory and practice of dark blue College Chapter 3 laser radar distortion removal exercise
- I/O多路复用及其相关详解
- Generate verification code
- re正则表达式
- Tell the truth of TS
- Solving the problem of displaying too many unique values in ArcGIS partition statistics failed
- Re regular expression
- The method of changing a value in the array and a value in the object of wechat applet
- 读取excel,int 数字时间转时间
- Classification of cifar100 data set based on convolutional neural network
猜你喜欢
Go对文件操作
The ultimate experience, the audio and video technology behind the tiktok
Go's gin framework learning
ArcGIS license error -15 solution
Nat commun | current progress and open challenges of applied deep learning in Bioscience
MySQL 中的字符串函数
C1小笔记【任务训练篇一】
[UDS unified diagnostic service] (Supplement) v. detailed explanation of ECU bootloader development points (2)
positioner
2022江西储能技术展会,中国电池展,动力电池展,燃料电池展
随机推荐
云原生虚拟化:基于 Kubevirt 构建边缘计算实例
Realsense selection comparison d455 d435i d415 t265 3D hardware comparison
Visualization of residential house prices
Click Cancel to return to the previous page and modify the parameter value of the previous page, let pages = getcurrentpages() let prevpage = pages [pages. Length - 2] / / the data of the previous pag
Go language JSON package usage
Eigen learning summary
[UDS unified diagnostic service] (Supplement) v. detailed explanation of ECU bootloader development points (2)
Halo 开源项目学习(二):实体类与数据表
Map basemap Library
Docker 安装 Redis
JS get link? The following parameter name or value, according to the URL? Judge the parameters after
Crawl the product data of cicada mother data platform
The ultimate experience, the audio and video technology behind the tiktok
xlsxwriter. exceptions. Filecreateerror: [errno 13] permission denied
Timestamp to formatted date
Theory and practice of laser slam in dark blue College - Chapter 2 (odometer calibration)
Re expression régulière
Remember using Ali Font Icon Library for the first time
[UDS unified diagnostic service] (Supplement) v. detailed explanation of ECU bootloader development points (1)
The JS timestamp of wechat applet is converted to / 1000 seconds. After six hours and one day, this Friday option calculates the time