一、什么是协程
协程(coroutine),也可以被称为微线程,是一种用户态内的上下文切换技术,简而言之,其实就是通过一个线程实现代码块相互执行,例如:
def func1():
print(1)
...
print(2)
def func2():
print(3)
...
print(4)
func1()
func2()
- 实现协程的几种方法
- greenlet 早起模块
- yield 关键字
- asyncio 装饰器(py3.4)
- async、await (py3.5 推荐)
########各种方式实现协程############
###1、使用greelet#####################################
#pip install greenlet
from greenlet import greenlet
def func1():
print(1) #第二步:输出1
gr2.switch() #第三步:切换到func2 函数
print(2) #第六步:输出3
gr2.switch() #第七部:切换到fun2 函数,从上一次执行的地方继续向后执行
def func2():
print(3) #第四步:输出3
gr1.switch() #第五步:切换到func1函数,从上一次执行的地方继续向后执行
print(4) #第八步:输出4
gr1 = greenlet(func1())
gr2 = greenlet(func2())
gr1.switch() #第一步:去执行func1函数
### 2、使用yield关键字#########################################
def func1():
yield 1
yield from func2()
yield 2
def func2():
yield 3
yield 4
f1 = func1()
for item in f1:
print(item)
### 3.使用asynico############################################
import asyncio
@asyncio.coroutine
def func1():
print(1)
yield from asyncio.sleep(2) #遇到io耗时操作,自动切换到task中的其他任务
print(2)
@asyncio.coroutine
def func2():
print(3)
yield from asyncio.sleep(2) # 遇到io耗时操作,自动切换到task中的其他任务
print(4)
tasks = [
asyncio.ensure_future(func1()),
asyncio.ensure_future(func2())
]
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.wait(tasks))
#############打印结果
1
3
2
4
Process finished with exit code 0
### 4.使用async & await关键字
import asyncio
async def func1():
print(1)
await asyncio.sleep(2) #遇到io耗时操作,自动切换到task中的其他任务
print(2)
async def func2():
print(3)
await asyncio.sleep(2) # 遇到io耗时操作,自动切换到task中的其他任务
print(4)
tasks = [
asyncio.ensure_future(func1()),
asyncio.ensure_future(func2())
]
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.wait
二、协程的意义
在一个线程中,如果遇到一个io等待的时间,线程不会傻等,利用空闲的时间去做其他的事情。
实例:下载图片为例
### 普通方式(同步)
import requests
def download_iamge(url):
print('开始下载:',url)
response = requests.get(url)
print('下载完成')
# 图片保存到本地
file_name = url.rsplit('=')[-1] + '.jpg'
with open(file_name,'wb') as f:
f.write(response.content)
if __name__ == '__main__':
url_list = [
'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fa0.att.hudong.com%2F52%2F62%2F31300542679117141195629117826.jpg&refer=http%3A%2F%2Fa0.att.hudong.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1618321273&t=6ec9fb79c23c0ebd3d7654adc7406af7',
'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fa4.att.hudong.com%2F20%2F39%2F01300542519189139990390839214.jpg&refer=http%3A%2F%2Fa4.att.hudong.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1618321273&t=4c9c469ac4e3a678376c0fc79b04c679',
'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic25.nipic.com%2F20121107%2F8847866_164210379199_2.jpg&refer=http%3A%2F%2Fpic25.nipic.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1618321273&t=9dd205cd023a8f4792d69dca4336f327'
]
for i in url_list:
download_iamge(i)
###################运行结果
开始下载: https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fa0.att.hudong.com%2F52%2F62%2F31300542679117141195629117826.jpg&refer=http%3A%2F%2Fa0.att.hudong.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1618321273&t=6ec9fb79c23c0ebd3d7654adc7406af7
下载完成
开始下载: https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fa4.att.hudong.com%2F20%2F39%2F01300542519189139990390839214.jpg&refer=http%3A%2F%2Fa4.att.hudong.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1618321273&t=4c9c469ac4e3a678376c0fc79b04c679
下载完成
开始下载: https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic25.nipic.com%2F20121107%2F8847866_164210379199_2.jpg&refer=http%3A%2F%2Fpic25.nipic.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1618321273&t=9dd205cd023a8f4792d69dca4336f327
下载完成
Process finished with exit code 0
############## 协程(异步) #################################
import asyncio
import aiohttp
async def fetch(session,url):
print('发送请求',url)
async with session.get(url,verify_ssl=False) as response:
content = await response.content.read()
file_name = url.rsplit('=')[-1]+ '.jpg'
with open(file_name,'wb') as f:
f.write(content)
print('下载完成')
async def main():
async with aiohttp.ClientSession() as session:
url_list = [
'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fa0.att.hudong.com%2F52%2F62%2F31300542679117141195629117826.jpg&refer=http%3A%2F%2Fa0.att.hudong.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1618321273&t=6ec9fb79c23c0ebd3d7654adc7406af7',
'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fa4.att.hudong.com%2F20%2F39%2F01300542519189139990390839214.jpg&refer=http%3A%2F%2Fa4.att.hudong.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1618321273&t=4c9c469ac4e3a678376c0fc79b04c679',
'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic25.nipic.com%2F20121107%2F8847866_164210379199_2.jpg&refer=http%3A%2F%2Fpic25.nipic.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1618321273&t=9dd205cd023a8f4792d69dca4336f327'
]
tasks = []
for url in url_list:
task = asyncio.create_task(fetch(session,url))
tasks.append(task)
# tasks = [ asyncio.create_task(fetch(session,url)) for url in url_list]
await asyncio.wait(tasks)
if __name__ == '__main__':
asyncio.run(main())
###### 运行结果
发送请求 https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fa0.att.hudong.com%2F52%2F62%2F31300542679117141195629117826.jpg&refer=http%3A%2F%2Fa0.att.hudong.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1618321273&t=6ec9fb79c23c0ebd3d7654adc7406af7
发送请求 https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fa4.att.hudong.com%2F20%2F39%2F01300542519189139990390839214.jpg&refer=http%3A%2F%2Fa4.att.hudong.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1618321273&t=4c9c469ac4e3a678376c0fc79b04c679
发送请求 https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic25.nipic.com%2F20121107%2F8847866_164210379199_2.jpg&refer=http%3A%2F%2Fpic25.nipic.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1618321273&t=9dd205cd023a8f4792d69dca4336f327
下载完成
下载完成
下载完成
Process finished with exit code 0
三、异步编程
- 3.1、时间循环
可以理解成一个死循环,去检测并执行某些diamante
import asynico
#去生成或获取一个事件循环
loop = asynico.get_event_loop()
#将任务放到 ‘任务列表’
loop.run_until_complete(任务)
- 3.2、async
协程函数:定义函数的时候,async def 函数名。
协程对象:执行协程函数()得到的协程对象
async def func():
print('test')
result = func()
注意:执行协程函数创建协程对象,函数内部代码不会执行。
如果想要运行函数内部代码,必须要将协程对象交给时间循环来处理
async def func():
print('test')
result = func()
#loop = asyncio.get_event_loop()
#loop.run_until_complete(result)
#python3.7以后可以用
asyncio.run(result)
- 3.3、await
await + 可等待的对象(协程对象、future、task对象–>io等待)
示例1:
import asyncio
async def func():
print('开始')
response = await asyncio.sleep(2)
print('结束')
asyncio.run(func)
实例:2:
import asyncio
async def other():
print('start')
await asyncio.sleep(2)
print('返回值')
async def func():
print('执行函数内部代码')
#遇到IO操作挂起当前协程(任务),等io操作完之后在继续往下执行,当前协程挂起时,事件循环可以去执行其他的协程
response = await other()
print('io请求结束,结果为:',response)
asyncio.run(func())
###########运行结果#############
执行函数内部代码
start
返回值
io请求结束,结果为: None
Process finished with exit code 0
示例3:
import asyncio
async def other():
print('start')
await asyncio.sleep(2)
print('返回值')
async def func():
print('执行函数内部代码')
#遇到IO操作挂起当前协程(任务),等io操作完之后在继续往下执行,当前协程挂起时,事件循环可以去执行其他的协程
response1 = await other()
print('io请求结束,结果为:',response1)
response2 = await other()
print('io请求结束,结果为:', response2)
asyncio.run(func())
#################运行结果##################
执行函数内部代码
start
返回值
io请求结束,结果为: None
start
返回值
io请求结束,结果为: None
Process finished with exit code 0
await就是等待对象的值得到结果之后再继续向下走
- 3.4、task对象
在事件循环中添加多个任务
官方:task用于并发调度协程,通过asyncio.create_task(协程对象)的方式创建task对象,这样可以让协程加入事件循环中等待被调度执行,除了使用asyncio.create_task() 函数以外,还可以使用低层级的loop.create_task() 或 ensure_future() 函数。不建议手动实例化task对象。
示例1:
import asyncio
async def fun1():
print('start')
await asyncio.sleep(2)
print('end')
return '返回值'
async def main():
print('main开始')
#创建task对象,将当前执行func函数添加到事件循环
task1 = asyncio.create_task(fun1())
task2 = asyncio.create_task(fun1())
print('main结束')
result1 = await task1
result2 = await task2
print(result1,result2)
asyncio.run(main())
#################运行结果###################
main开始
main结束
start
start
end
end
返回值 返回值
Process finished with exit code 0
示例2:常用方法
import asyncio
async def fun1():
print('start')
await asyncio.sleep(2)
print('end')
return '返回值'
async def main():
print('main开始')
#创建task对象,将当前执行func函数添加到事件循环
task_list = [
asyncio.create_task(fun1()),
asyncio.create_task(fun1())
]
print('main结束')
done,pending = await asyncio.wait(task_list,timeout=None)
print(done)
asyncio.run(main())
示例3:
import asyncio
async def fun1():
print('start')
await asyncio.sleep(2)
print('end')
return '返回值'
task_list = [
fun1(),
fun1(),
]
done = asyncio.run(asyncio.wait(task_list))
print(done)
- 3.5、Future对象
task集成future,task对象内部await结果的处理基于future对象。
示例1:
import asyncio
async def main():
#获取当前事件循环
loop = asyncio.get_running_loop()
#创建一个任务(future对象),这个任务什么也不做
fut = loop.create_future()
#等待任务最终结束(future对象),没有结果就一直等下去
await fut
asyncio.run(main())
示例2:
import asyncio
async def set_after(fut):
await asyncio.sleep(2)
fut.set_result('33')
async def main():
# 获取当前事件循环
loop = asyncio.get_running_loop()
#创建一个任务(future对象),这个任务什么也不做
fut = loop.create_future()
#创建一个任务(task对象),绑定了set_after函数,函数内部在2s之后,会给fut赋值
#即手动设置future任务的最终结果,那么fut就可以结束了
await loop.create_task(set_after(fut))
#等待future对象获取最终结果,否则一直等下去
data = await fut
print(data)
asyncio.run(main())
- 3.6、concurrent.future.Future 对象
使用线程池,进程池实现异步操作时用到的对象
import time
from concurrent.futures import Future
from concurrent.futures.thread import ThreadPoolExecutor
from concurrent.futures.process import ProcessPoolExecutor
def func(value):
time.sleep(2)
print(value)
#创建线程池
pool = ThreadPoolExecutor(max_workers=5)
#创建进程池
# pool = ProcessPoolExecutor(max_workers=5)
for i in range(10):
fut = pool.submit(func,i)
print(fut)
以后写代码可能会存在交叉时间,例如:crm项目80%都基于协程异步编程+mysql(不支持)【线程、进程做异步编程】
示例:
import time
import asyncio
import concurrent.futures
def func1():
#某个耗时操作
time.sleep(2)
return 'test'
async def main():
loop = asyncio.get_event_loop()
#1.run in the default loop's executor(默认threapoolexecutor)
#第一步:内部会先调用ThreadpoolExecutor 的submit 方法去线程池中申请一个线程去执行func1函数,并返回一个
#concurrent.future.Future对象
#第二步:调用asyncio.wrap_future将concurrent.future.Future对象包装为asyncio.Future对象
#因为concurrent.future.Future对象不支持await语法,所以需要包装为asyncio.future对象才能使用
fut = loop.run_in_executor(None,func1)
result = await fut
print('default thread pool',result)
#run in a custom thread pool
# with concurrent.futures.ThreadPoolExecutor() as pool:
# result = await loop.run_in_executor(pool,func1)
# print('default thread pool',result)
#run in a custom process pool
# with concurrent.futures.ProcessPoolExecutor() as pool:
# result = await loop.run_in_executor(pool,func1)
# print('default thread pool', result)
asyncio.run(main())
案例:asyncio+不支持异步的模块
import asyncio
import requests
async def download_image(url):
print('下载开始:',url)
loop = asyncio.get_event_loop()
#requests模块默认不支持异步操作,所以就直接用线程池配合实现
future = loop.run_in_executor(None,requests.get(url))
response = await future
print('下载完成')
#将图片保存本地
filename = url.replit('_')[-1]
with open(filename,'wb')as f:
f.write(response.content)
if __name__ == '__main__':
url_list = [
'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fa0.att.hudong.com%2F52%2F62%2F31300542679117141195629117826.jpg&refer=http%3A%2F%2Fa0.att.hudong.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1618321273&t=6ec9fb79c23c0ebd3d7654adc7406af7',
'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fa4.att.hudong.com%2F20%2F39%2F01300542519189139990390839214.jpg&refer=http%3A%2F%2Fa4.att.hudong.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1618321273&t=4c9c469ac4e3a678376c0fc79b04c679',
'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic25.nipic.com%2F20121107%2F8847866_164210379199_2.jpg&refer=http%3A%2F%2Fpic25.nipic.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1618321273&t=9dd205cd023a8f4792d69dca4336f327'
]
task = [download_image(url) for url in url_list]
loop = asyncio.get_event_loop()
loop.run_until_complete(task)
- 3.7、异步迭代器
- 什么是异步迭代器:
实现了 aiter() 和 anext() 方法的对象。 __anext__必须返回一个 awaitable 对象,async for 会处理异步迭代器,anext() 方法返回的可等待对象,知道其引来发一个 stopAsyncIteration 异常 - 什么是异步迭代对象:
可在async for 语句中被使用的对象,必须通过__aiter__() 方法返回一个asynchronous iterator。
- 什么是异步迭代器:
import asyncio
class reader(object):
#自定义迭代器(同时也是异步可迭代对象)
def __init__(self):
self.count = 0
async def readline(self):
self.count += 1
if self.count == 100:
return None
return self.count
def __aiter__(self):
return self
async def __anext__(self):
val = await self.readline()
if val == None:
raise StopAsyncIteration
return val
async def func():
obj = reader()
async for item in obj:
print(item)
asyncio.run(func())
- 3.8、异步上下文管理器
此种对象通过定义 aenter() 和 aexit() 方法来对 async with 语句中的环境进行控制
import asyncio
class AsyncContextManager:
def __init__(self):
print('in init')
async def do_somethig(self):
#异步操作数据库
return '666'
async def __aenter__(self):
#异步连接数据库
print('in aenter')
return self
async def __aexit__(self, exc_type, exc_val, exc_tb):
#异步关不数据库连接
await asyncio.sleep(1)
print('in aexit')
async def func():
async with AsyncContextManager() as f:
result = await f.do_somethig()
print(result)
asyncio.run(func())
四、uvloop
是asyncio的事件循环的替代方案。事件循环>默认asyncio的事件循环
pip install uvloop
import asyncio
import nvloop
asyncio.set_event_loop(uvloop.EventLoopPolicy())
#编写asyncio的代码
#内部的事件循环自动化会变成uvloop
asyncio.run()
五、实战练习
- 异步Redis
pip install aioredis
import asyncio
import aioredis
async def execute(address,password):
print('开始执行',address)
#网络IO操作:先去连接47.93.2.123:6368 遇到io则自动切换任务,去连接47.93.2.124:6369
redis = await aioredis.create_redis_pool(address=address,password=password)
#网络IO操作:遇到io则自动切换任务
#在Redis中设置哈希值car,内部再设置三个键值对
await redis.hmset_dict('car',key1=1,key2=2,key3=3)
# 网络IO操作:遇到io则自动切换任务
#去Redis中获取值
result = await redis.hgetall('car',encoding='utf-8')
print(result)
redis.close()
# 网络IO操作:遇到io则自动切换任务,关闭Redis连接
await redis.wait_closed()
print('结束',address)
task_list = [
execute('redis://47.93.2.123:6368','root123'),
execute('redis://47.93.2.124:6369','root123')
]
asyncio.run(asyncio.wait(task_list))
- 异步操作mysql
pip install aiomysql
import asyncio
import aiomysql
async def execute(host,password):
print('开始',host)
#网络io操作,先去连接47.93.20.99 遇到io则自动切换任务,连接47.93.20.100
conn = await aiomysql.connect(host=host,port=3306,user='root',password=password)
#网络io操作, 遇到io则自动切换任务,创建cursor
cur = await conn.cursor()
# 网络io操作, 遇到io则自动切换任务,执行SQL
await cur.execute('select * from use')
# 网络io操作, 遇到io则自动切换任务,获取SQL结果
result = await cur.fetchall()
print(result)
# 网络io操作, 遇到io则自动切换任务,关闭连接
await cur.close()
conn.close()
print('结束',host)
task_list = [
execute('47.93.20.99','root123'),
execute('47.93.20.100','root123')
]
asyncio.run(asyncio.wait(task_list))
- 爬虫
pip install aiohttp
import aiohttp
import asyncio
async def fetch(session,url):
print('发送请求:',url)
async with session.get(url,verify_ssl = False) as response:
text = await response.text()
print('得到结果:',url,len(text))
return text
async def main():
async with aiohttp.ClientSession() as session:
url_list = [
'https://python.org',
'https://baidu.com',
'https://pythonav.com'
]
task = [ asyncio.create_task( fetch(session,url)) for url in url_list]
done,pending = await asyncio.wait(task)
if __name__ == '__main__':
asyncio.run(main())