摘要
主要介绍python中相关的异步编程的原理和是代码的实战
协程实现
协程(Coroutine),也可以被称为微线程,是一种用户态内的上下文切换技术。简而言之,其实就是通过一个线程实现代码块相互切换执行。协程不是计算机提供,程序员人为创造。
协程的优点:在一个线程中如果遇到IO等待时间,线程不会傻傻等,利用空闲的时候再去干点其他事。
greenlet 实现协程
from greenlet import greenlet
def fun1():
print(1)
gr2.switch()
print(2)
gr2.switch()
def fun2():
print(3)
gr1.switch()
print(4)
gr1 = greenlet(fun1)
gr2 = greenlet(fun2)
gr1.switch()
yield 关键字(在企业的代码不会使用)
def fun3():
yield 1
yield from fun4()
yield 2
def fun4():
yield 3
yield 4
f = fun3()
for item in f:
print(item)
#显示的结果
----------------------------------------------------
1
3
4
2
asyncio 装饰器(py3.4)
import asyncio
@asyncio.coroutines
def fun5():
print(1)
yield from asyncio.sleep(2) # 遇到IO的操作的时候自动切换到task中的其他任务
print(2)
@asyncio.coroutines
def fun6():
print(3)
yield from asyncio.sleep(2) # 遇到IO的操作的时候自动切换到task中的其他任务
print(4)
task = [
asyncio.ensure_future(fun5()),
asyncio.ensure_future(fun6())
]
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.wait(task))
#显示结果
1
3
2
4
async & await关键字(py3.5)推荐的使用
import asyncio
async def fun7():
print(7)
await asyncio.sleep(2) # 遇到IO的操作的时候自动切换到task中的其他任务
print(8)
async def fun8():
print(9)
await asyncio.sleep(2) # 遇到IO的操作的时候自动切换到task中的其他任务
print(10)
task = [
asyncio.ensure_future(fun7()),
asyncio.ensure_future(fun8())
]
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.wait(task))
图片下载实战
采用的普通方式进行图片的下载
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@version: 1.0
@author: xjl
@file: 图片的下载.py
@time: 2021/9/12 16:49
"""
import requests
def download_images(url):
print("开始下载", url)
response = requests.get(url)
print("下载完成")
# 保存文件到本地中
file_name = url.rsplit('_')[-1]
with open(file_name, mode='wb') as file_object:
file_object.writable(response.content)
print("保存图片的功能完成")
if __name__ == "__main__":
url_list = [
'https://img.alicdn.com/imgextra/i3/127167800/O1CN01lywipS27US0reYkHE_!!0-saturn_solar.jpg_468x468q75.jpg',
'https://img.alicdn.com/imgextra/i2/127167800/O1CN01M4cuWt27US1BH6Ke6_!!0-saturn_solar.jpg_468x468q75.jpg',
'https://img.alicdn.com/imgextra/i4/206720011/O1CN01HKfJ251Bx53r6kuqZ_!!0-saturn_solar.jpg_468x468q75.jpg'
]
for item in url_list:
download_images(item)
采用的异步(协程的方式)方式进行图片的下载
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@version: 1.0
@author: xjl
@file: 图片的下载异步操作.py
@time: 2021/9/12 17:04
"""
import aiohttp
import asyncio
async def fetch(session, url):
print("发送请求")
async with session.get(url, verify_ssl=False) as response:
content = await response.content.read()
file_name = url.rsplit('_')[-1]
with open(file_name, mode='wb') as file_object:
file_object.write(content)
print("下载完成")
async def main():
async with aiohttp.ClientSession() as session:
url_list = [
'https://img.alicdn.com/imgextra/i3/127167800/O1CN01lywipS27US0reYkHE_!!0-saturn_solar.jpg_468x468q75.jpg',
'https://img.alicdn.com/imgextra/i2/127167800/O1CN01M4cuWt27US1BH6Ke6_!!0-saturn_solar.jpg_468x468q76.jpg',
'https://img.alicdn.com/imgextra/i4/206720011/O1CN01HKfJ251Bx53r6kuqZ_!!0-saturn_solar.jpg_468x468q85.jpg'
]
task = [asyncio.get_event_loop().create_task(fetch(session, url)) for url in url_list]
await asyncio.wait(task)
if __name__ == '__main__':
# asyncio.run(main()) 同文件夹下有 asyncio.py
asyncio.get_event_loop().run_until_complete(main())
【路飞】Python异步编程2020年最新版【asyncio】【异步编程】【协程】_哔哩哔哩_bilibili
标签:异步,task,python,编程,jpg,url,print,def,asyncio From: https://blog.51cto.com/u_13643065/6169205