-
Notifications
You must be signed in to change notification settings - Fork 0
/
chain_coro.py
59 lines (44 loc) · 1.77 KB
/
chain_coro.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import asyncio
import aiohttp
from functools import partial
import logging
logging.basicConfig(level=logging.DEBUG)
logging.getLogger('asyncio').setLevel(logging.DEBUG)
SRCS = ['http://cdn.daweijita.com/2016/06/tab_weiwenhuaming_secret-base_tanchang_1.gif',
'http://cdn.daweijita.com/2016/06/tab_weiwenhuaming_secret-base_tanchang_2.gif',
'http://cdn.daweijita.com/2016/06/tab_weiwenhuaming_secret-base_tanchang_3.gif',
'http://cdn.daweijita.com/2016/06/tab_weiwenhuaming_secret-base_tanchang_4.gif',
'http://cdn.daweijita.com/2016/06/tab_weiwenhuaming_secret-base_tanchang_5.gif',
'http://cdn.daweijita.com/2016/06/tab_weiwenhuaming_secret-base_tanchang_6.gif'
]
def save_image(): # 用一个闭包做文件名的区分,没吊用一次加一
count = 0
def save(image):
nonlocal count
path = './'+str(count)+'.gif'
count += 1
with open(path, 'wb') as fp:
fp.write(image)
return save
async def get_image(url): # 下载图片
session = aiohttp.ClientSession()
resp = await session.get(url)
image = await resp.read()
session.close()
return image
async def download_one(url, save): # 下载并保存一个图片, 链式协程
image = await get_image(url)
save(image)
def main():
loop = asyncio.get_event_loop()
loop.set_debug(True)
si = save_image() # 拿到闭包
wait = asyncio.wait(map(partial(download_one, save=si), SRCS)) # 获取所有的src, 这里map函数得到一个获取各个图片的协程集合,一个可迭代对象
# gather = asyncio.gather(*map(partial(download_one, save=si), SRCS))
try:
loop.run_until_complete(wait) #
finally:
loop.close()
loop.stop()
if __name__ == '__main__':
main()