浅谈Python协程asyncio
作者:小玖工作坊 发布时间:2021-01-12 21:30:32
一、协程
官方描述;
协程是子例程的更一般形式。 子例程可以在某一点进入并在另一点退出。 协程则可以在许多不同的点上进入、退出和恢复。 它们可通过 async def 语句来实现。 参见 PEP 492。
协程不是计算机内部提供的,不像进程、线程,由电脑本身提供,它是由程序员人为创造的, 实现函数异步执行。
协程(Coroutine),也可以被称为微线程,是一种用户太内的上下文切换技术,其实就是通过一个线程实现代码块相互切换执行。看上去像子程序,但执行过程中,在子程序内部可中断,然后转而执行别的子程序,在适当的时候再返回来接着执行。例如:
# 需要python3.7+
import asyncio
async def main():
print('hello')
await asyncio.sleep(1)
print('world')
asyncio.run(main())
# 打印 "hello",等待 1 秒,再打印 "world"
注意:简单地调用一个协程并不会使其被调度执行,
直接main() 调用会有问题:
RuntimeWarning: coroutine 'main' was never awaited
main()
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
def func1():
print(1)
...
print(2)
def func2():
print(3)
...
print(4)
func1()
func2()
# 结果:1 2 3 4
实现协程的方法:
greenlet,早期模块【不建议使用】
yield关键字,它是python的生成器,具有保存状态,切换到其他函数去执行,再切换回原函数的功能。
asyncio装饰器(python3.4引入)
async、await 关键字(python3.5)【推荐使用】
1.1 greenlet实现协程
# 第三方模块,因此需要安装
pip install greenlet
from greenlet import greenlet
def func1():
print(1)
gr2.switch()
print(2)
gr2.switch()
def func2():
print(3)
gr1.switch()
print(4)
gr1 = greenlet(func1)
gr2 = greenlet(func2)
gr1.switch()
# 结果:1 3 2 4
1.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)
# 结果:1 3 2 4
1.3 asynico装饰器
python3.4 及之后版本支持
DeprecationWarning: “@coroutine” decorator is deprecated since Python 3.8, use “async def”
翻译:@coroutine"装饰器自Python 3.8起已弃用,请使用"async def"代替
所以这个也不支持。
import asyncio
@asyncio.coroutine
def func1():
print(1)
yield from asyncio.sleep(2) # 遇到IO耗时操作,自动切换到tasks中其他任务,比如:网络IO,下载图片
print(2)
@asyncio.coroutine
def func2():
print(3)
yield from asyncio.sleep(2) # 遇到IO耗时操作,自动切换到tasks中其他任务,比如:网络IO,下载图片
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
1.4 async & await 关键字
import asyncio
async def func1():
print(1)
await asyncio.sleep(2) # 遇到IO耗时操作,自动切换到tasks中其他任务,比如:网络IO,下载图片
print(2)
async def func2():
print(3)
await asyncio.sleep(2) # 遇到IO耗时操作,自动切换到tasks中其他任务,比如:网络IO,下载图片
print(4)
tasks = [
asyncio.ensure_future(func1()),
asyncio.ensure_future(func2())
]
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.wait(tasks))
二、协程的意义
充分利用线程。在一个线程中如果遇到IO等待时间线程不会一直等待,利用空闲时间再去干点其他事情。
以下载三张图片为例:
普通方式(同步)下载:
import time
import requests
def download_image(url, img_name):
print("开始下载:", url)
# 发送网络请求,下载图片
response = requests.get(url)
print("下载完成")
# 图片保存到本地文件
file_name = str(img_name) + '.png'
with open(file_name, mode='wb') as file:
file.write(response.content)
if __name__ == '__main__':
start = time.time()
url_list = [
'https://tse4-mm.cn.bing.net/th/id/OIP.866vRxQ8QvyDsrUuXiu7qwHaNK?w=182&h=324&c=7&o=5&pid=1.7',
'https://tse2-mm.cn.bing.net/th/id/OIP.HUcWtoYPG-z2pu4ityajbAHaKQ?w=182&h=252&c=7&o=5&pid=1.7',
'https://tse2-mm.cn.bing.net/th/id/OIP.MvncR0-Pt9hVxKTdrvD9dAHaNK?w=182&h=324&c=7&o=5&pid=1.7',
'https://tse1-mm.cn.bing.net/th/id/OIP._nGloaeMWbL7NB7Lp6SnXQHaLH?w=182&h=273&c=7&o=5&pid=1.7',
]
img_name = 1
for item in url_list:
download_image(item, img_name)
img_name += 1
end = time.time()
print(end - start)
# 最终时间:7.25s
协程方式(异步)下载:
import aiohttp
import asyncio
import time
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]
# print(file_name)
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://www3.autoimg.cn/newsdfs/g26/M02/35/A9/120x90_0_autohomecar__ChsEe12AXQ6AOOH_AAFocMs8nzU621.jpg',
'https://www3.autoimg.cn/newsdfs/g26/M02/35/A9/120x90_0_autohomecar__ChsEe12AXQ6AOOH_AAFocMs8nzU621.jpg',
'https://www3.autoimg.cn/newsdfs/g26/M02/35/A9/120x90_0_autohomecar__ChsEe12AXQ6AOOH_AAFocMs8nzU621.jpg',
'https://www3.autoimg.cn/newsdfs/g26/M02/35/A9/120x90_0_autohomecar__ChsEe12AXQ6AOOH_AAFocMs8nzU621.jpg',
]
tasks = [asyncio.ensure_future(fetch(session, url)) for url in url_list]
await asyncio.wait(tasks)
if __name__ == '__main__':
start = time.time()
asyncio.get_event_loop().run_until_complete(main())
end = time.time()
print(end - start)
# 结果:0.05s
来源:https://blog.csdn.net/zhb_feng/article/details/117930310
猜你喜欢
- 你还没用 jQuery 写过导航菜单? 相信看到这些出色的jQuery导航菜单后,一定会为此而后悔没早点把 jQuery 应用到自己的Web
- 1.echo和print的区别PHP中echo和print的功能基本相同(输出),但是两者之间还是有细微差别的。echo输出后没有返回值,但
- conn.execute、rs.open之间的差别,conn.execute、rs.open、command.execute方法用法大大不同
- 本章的前面讨论如何使用SQL向一个表中插入数据。但是,如果你需要向一个表中添加许多条记录,使用SQL语句输入数据是很不方便的。幸运的是,My
- 你不可能随时备份数据,但你的数据丢失时,或者数据库目录中的文件损坏时, 你只能恢复已经备份的文件,而在这之后的插入或更新的数据,就无能为力了
- 八种获取当前日期的js代码,第一步:把如下代码加入<head>区域中:<SCRIPT language=java
- 在数据库testDB中存在架构A及用户A,现将testDB数据库所属的用户由A改为B,同时删除用户A;架构也由A改为B,删除架构A,操作如下
- 对于数字索引数组来说,通过 array_push()函数向数组中添加元素。array_push()函数将数组当成一个栈,将传入的变量压入该数
- 印象中最早看老外个人网站就挺纳闷,怎么人家都没有www,这样也可以?经过不断尝试,我发现确实不录入www要快捷的多,但不清楚怎么能做到。几年
- 大家好,学完面向对象与异常处理机制之后,接下里我们要学习 包与模块 。首先我们要了解什么是包?什么是模块?接下来我们还要学习 如何自定义创建
- 404页面对于站长来说应该并不陌生,其作用无碍乎二点:提高用户体验和增强对搜索引擎的友好性。去年在跟几个朋友在聊天的时候,跟我说404页面不
- CSS布局作为一个热门技术,发展的确有些缓慢。CSS最早被提议在1994年,最早被浏览器支持在1996年,CSS一直被力捧为传统的以HTML
- 一.文件基本操作1.1 文件打开和关闭open(‘文件名称','打开模式')模式:r(只读,指针再头) w(只写,存
- 问:怎样才能取得局域网中所有SQL Server的实例?答:请参考以下的具体步骤:SmoApplication.EnumAvailableS
- 异步编程一直是JavaScript 编程的重大事项。关于异步方案, ES6 先是出现了 基于状态管理的 Promise,然后出现了 Gene
- 本文总结了ASP初学者常犯的几个错误,希望对asp学习者有所帮助!1.记录集关闭之前再次打开:-----------------------
- 1. ASP与Access数据库连接: 代码如下:dim strConn dim conn strConn = "Provide
- //创建组件 function createobj() { if (window.ActiveXObject) { return(new A
- 最近在研究tensorflow自带的例程speech_command,顺便学习tensorflow的一些基本用法。其中tensorboard
- 实现代码/// <summary>/// 去除HTML标记/// </summary&