想让大佬看看我的协程异步哪里出错了!
帮忙看看我的程序,同样的需求,用异步和同步花的时间是一样的!import time
import requests
from lxml import etree
import asyncio
start = time.time()
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36'}
#获取协程
async def landscape(url):
landscape_page = requests.get(url=url,headers=headers).text
land_tree = etree.HTML(landscape_page)
landscape_li = land_tree.xpath('//div[@class="slist"]/ul/li')
for landscape_li in landscape_li:
land_url = landscape_li.xpath('./a/img/@src')
land_str = ''.join(land_url) #将列表转换成字符
land_right_url = 'http://pic.netbian.com' + land_str
land_name = landscape_li.xpath('./a/img/@alt')
land_right_name = ''.join(land_name) + '.jpg' #将名称列表转化名称字符
landscape_png = requests.get(url=land_right_url, headers=headers).content
with open(r'C:\Users\Administrator\Desktop\4k风景\{}'.format(land_right_name), 'wb') as fp:
fp.write(landscape_png)
print(land_right_name + '下载成功!')
#获取所有风景页面的url
urls = []
for num in range (2,6):
landscape_url = 'http://pic.netbian.com/4kfengjing/index_'+str(num)+'.html'
urls.append(landscape_url)
#获取task任务对象,并且将协程对象封装到任务对象
tasks = []
for url in urls:
xiecheng = landscape(url)
task = asyncio.ensure_future(xiecheng)
tasks.append(task)
#事件环
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.wait(tasks))
end = time.time()
print(end - start) 可能是因为GIL 分析了一下代码里最花费时间的是requests.get和open
这两个函数能await吗 如果不能await根本不能节省时间
就像是time.sleep和await asyncio.sleep, 后者才能节省时间 requests换成aiohttp模块就可以了( 我今晚试试 问题已解决,使用aiohttp模块发起请求即可,记得挂起哦
页:
[1]