当前位置:网站首页>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
边栏推荐
- C1小笔记【任务训练篇二】
- C#的随机数生成
- Implementation of object detection case based on SSD
- Halo 开源项目学习(二):实体类与数据表
- Examination question bank and online simulation examination of the third batch (main person in charge) of special operation certificate of safety officer a certificate in Guangdong Province in 2022
- 2022 Shanghai safety officer C certificate operation certificate examination question bank and simulation examination
- Scikit learn sklearn 0.18 official document Chinese version
- Pyppeter crawler
- Nat Commun|在生物科学领域应用深度学习的当前进展和开放挑战
- Crack sliding verification code
猜你喜欢

C1小笔记【任务训练篇二】

Halo 开源项目学习(二):实体类与数据表

The ultimate experience, the audio and video technology behind the tiktok

C network related operations

ArcGIS table to excel exceeds the upper limit, conversion failed

MySQL_01_简单数据检索

2022 Jiangxi Photovoltaic Exhibition, China distributed Photovoltaic Exhibition, Nanchang solar energy utilization Exhibition

2022 Jiangxi energy storage technology exhibition, China Battery exhibition, power battery exhibition and fuel cell Exhibition

Eigen learning summary

Examination question bank and online simulation examination of the third batch (main person in charge) of special operation certificate of safety officer a certificate in Guangdong Province in 2022
随机推荐
2022 Jiangxi energy storage technology exhibition, China Battery exhibition, power battery exhibition and fuel cell Exhibition
Halo 开源项目学习(二):实体类与数据表
Cloud native Virtualization: building edge computing instances based on kubevirt
Excel opens large CSV format data
Error in created hook: "referenceerror:" promise "undefined“
.104History
C# 网络相关操作
Tell the truth of TS
Nat commun | current progress and open challenges of applied deep learning in Bioscience
C network related operations
Romance in C language
Oil monkey website address
Implementation of image recognition code based on VGg convolutional neural network
[UDS unified diagnostic service] (Supplement) v. detailed explanation of ECU bootloader development points (1)
.105Location
Summary of floating point double precision, single precision and half precision knowledge
MySQL_01_简单数据检索
Operators in C language
C language input and output (printf and scanf functions, putchar and getchar functions)
[UDS unified diagnostic service] IV. typical diagnostic service (6) - input / output control unit (0x2F)