Asyncio wait与gather的学习笔记
asyncio.wait与gather
最近在写一个网络爬虫,用asyncio、aiohttp来实现,主要目的是利用网络爬虫,将公司的陈年办公系统数据,爬取到一个简单的数据库结构中,忽略陈年办公系统的平台架构,只要数据。因为陈年办公系统的平台已经无用了,还废资源去保存整套架构,没有必要,而且如果平台出现硬件故障时,已经无人知晓如何恢复整个平台应用,因为技术储备已经淘汰了。
所以用网络爬虫是一个很好的选择。使用asyncio和aiohttp又是最优方案。其中wait和gather的使用又是其中的关键。这里我会借用其他作者的有关wait和gather的知识。
asyncio.wait
先看一段代码:定义3个异步调用的函数,然后在入口函数main(loop)中调用。
// An highlighted block
import asyncio
import time
def save_file(file,data):
time.sleep(2)
print("Hello {} save file success!".format((str(file))))
async def hello1(loop): # 大约10秒
print("Hello world 01 begin")
await asyncio.sleep(10)
await loop.run_in_executor(None,