Python爬虫-第四章-2-协程与异步

时间:2023-01-18 13:59:51

协程:

       单线程执行多任务执行时,当执行中程序处于I/O期间,异步可以让CPU选择性的切换到其他任务上

# Demo Describe:协程

import asyncio
import time

'''
协程所针对的问题:
1.类似input()或timeSleep函数,在程序运行中等待输入命令期间线程处于阻塞状态
2.类似requests.get(url)等网络请求时,在等待网络数据返回期间线程处于阻塞状态
问题总结:
一般情况下,当程序处于同步IO操作(input/output)时,线程处于阻塞状态,CPU不对当前程序负责,程序运行效率低下



# start--------1,简单示例----------------------
async def fun1():
print('fn1')
# time.sleep(3) # 同步操作
await asyncio.sleep(3) # 异步
print('fn1')


async def fun2():
print('fn2')
# time.sleep(2) # 同步操作
await asyncio.sleep(2) # 异步
print('fn2')


async def fun3():
print('fn3')
# time.sleep(4) # 同步操作
await asyncio.sleep(4) # 异步
print('fn3')


async def main():
f1 = asyncio.create_task(fun1())
f2 = asyncio.create_task(fun2())
f3 = asyncio.create_task(fun3())
task = [f1, f2, f3]
await asyncio.wait(task)


if __name__ == '__main__':
timeStart = time.time()
asyncio.run(main())
timeEnd = time.time()
print('程序执行时间:', timeEnd - timeStart)
'''
同步操作:
fn3
fn3
fn2
fn2
fn1
fn1
程序执行时间: 9.007003545761108
协程异步操作:
fn3
fn2
fn1
fn2
fn1
fn3
程序执行时间: 3.989386796951294
'''

# end--------1,简单示例----------------------

异步

# Demo Describe:aiohttp 异步Http请求操作

import aiohttp
import asyncio
import aiofiles as aiof

'''
本章内容:
requests.get()同步 变为 异步操作aiohttp
'''

urls = [
'https://pic.3gbizhi.com/2022/0402/20220402083510539.jpg',
'https://pic.3gbizhi.com/2021/1203/20211203082607923.jpg',
'https://pic.3gbizhi.com/2021/0928/20210928055411375.jpg'
]


async def AioDownLoad(url):
name = url.rsplit('/', 1)[1] # rsplit right 从右边切割
async with aiohttp.ClientSession() as session:
async with session.get(url) as resp:
content = await resp.content.read()
async with aiof.open(f'../FileForDemo/Pic3gbizhi/{name}.jpg', mode='wb') as file:
await file.write(content)
print('下载完毕!!!')


async def main():
tasks = []
for i in urls:
tasks.append(asyncio.create_task(AioDownLoad(i)))
await asyncio.wait(tasks)


if __name__ == '__main__':
asyncio.run(main())