1.协程

协程:当程序遇见了IO操作的时候,可以选择性的切换到
在微观上是一个任务的进行切换,切换条件一般的IO操作
在宏观上,我们能看到的其实是多个任务在一起执行
多任务异步操作
上方所讲一切,是在单线程条件下
import asyncio
import time
async def func():
print(1)
async def func1():
print(2)
await asyncio.sleep(2) # 异步阻塞
print(2)
async def func2():
print(3)
await asyncio.sleep(2) # 异步阻塞
print(3)
async def func3():
print(4)
await asyncio.sleep(2) # 异步阻塞
print(4)
async def main():
f1 = asyncio.create_task(func1()) #py3.8 后加上asyncio.create_task()
f2 = asyncio.create_task(func2())
f3 = asyncio.create_task(func3())
tasks = [
f1, f2, f3
]
await asyncio.wait(tasks)
if __name__ == '__main__':
q = func() # 此时函数是异步协程函数,函数执行得到的是一个协程对象
asyncio.run(q) # 协程程序运行需要asyncio模块的支持
t1 = time.time()
# 一次性启动多个任务
asyncio.run(main())
t2 = time.time()
print(t2-t1)
在爬虫领域的应用
# 在爬虫领域的应用
async def download(url):
print('准备开始下载')
await asyncio.sleep(2) # 网络请求
print('下载完成')
async def main():
urls = [
'http://www.baidu.com',
'http://www.163.com'
]
tasks = []
for url in urls:
d = download(url)
tasks.append(d)
await asyncio.wait(tasks)
2 异步请求aiohttp模块
import aiohttp
import asyncio
# resquests.get() 同步的代码 -》异步操作aiohttp
urls = [
'https://img1.baidu.com/it/u=1929941019,3324507395&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500',
]
async def download(url):
print('准备开始下载')
# 发送请求,得到图片内容,保存到文件
# aiohttp.ClientSession() 是异步的 requests()
s = aiohttp.ClientSession()
name = url.split('?')[-1]
async with s:
async with s.get(url) as resp:
# 请求回来写入文件
with open(name, mode='wb') as f:
f.write(await resp.content.read()) # 读写内容是异步的,需要await挂起, resp.text() resp.content.read()
print(name, '下载完成')
async def main():
tasks = []
for url in urls:
tasks.append(asyncio.create_task(download(url)))
await asyncio.wait(tasks)
if __name__ == '__main__':
# asyncio.run(main) 会自动关闭循环,报错
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
文章介绍了Python中的协程概念,如何在单线程中实现多任务异步操作,通过asyncio模块和async/await语法来调度协程任务。示例展示了在爬虫领域的应用,使用asyncio和aiohttp模块进行异步网络请求,提高爬取效率。
1389

被折叠的 条评论
为什么被折叠?



