请帮我看看为什么执行不到3,哪里有问题
import asyncio#协程处理模块import time
import aiohttp#异步请求页面模块
start = time.time()
urls=[
'www.sogou.com',
'www.baidu.com',
'www.goubanjia.com',
]
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36 Edg/91.0.864.67'
}
async def get_page(url,head):
print('执行到1')
async with aiohttp.ClientSession() as session:
print('执行到2')
async with await session.get(url=url,headers=head) as response:
print('执行到3')
page_text = await response.text()
print(page_text)
#任务列表,存放多个任务对象
tasks = []
for url in urls:
c = get_page(url,headers)
task = asyncio.ensure_future(c)
tasks.append(task)
loop = asyncio.get_event_loop()
#固定格式:将任务列表封装到wait方法中
loop.run_until_complete(asyncio.wait(tasks))
print('总耗时:',time.time()-start)
执行到1
执行到2
执行到1
执行到2
执行到1
执行到2
总耗时: 0.027003765106201172 你好像混合了新旧的两种写法。。
我觉得你可以参考这里的写法,逻辑比较清晰~
https://www.cnblogs.com/fengtao4918/p/14156313.html import asyncio
import time
import aiohttp
start = time.time()
urls = [
'http://127.0.0.1',
'http://127.0.0.1',
'http://127.0.0.1',
]
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36 Edg/91.0.864.67'
}
async def get_page(url, head):
print('执行到1')
async with aiohttp.ClientSession() as session:
print('执行到2')
async with session.get(url=url, headers=head) as response:
print('执行到3')
page_text = await response.text()
print(page_text[:15])
# 任务列表,存放多个任务对象
tasks = []
for url in urls:
tasks.append(get_page(url, headers))
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.gather(*tasks))
print('总耗时:', time.time()-start)
set PYTHONIOENCODING=utf8 && python "c:\Users\Administrator\Desktop\www\test.py"
执行到1
执行到2
执行到1
执行到2
执行到1
执行到2
执行到3
<!DOCTYPE html>
执行到3
<!DOCTYPE html>
执行到3
<!DOCTYPE html>
总耗时: 0.022365808486938477
exited with code=0 in 0.352 seconds
页:
[1]