python3.5+ asyncio await异步详解

import asyncio,time
from collections import defaultdict
from pprint import pprint

collect=defaultdict(list)

#######创建处理一个对象的方法,并返回期物
async def dealone(chunk):
	return 'Receive %d at %.5f seconds' % (chunk,time.time())

#######委派给dealone处理,并获取处理结果
async def submitor(chunk):
	future=await dealone(chunk)
	collect['res'].append(future)

chunks=range(10)

loop = asyncio.get_event_loop()
tasks=asyncio.wait([submitor(chunk) for chunk in chunks])
loop.run_until_complete(tasks)
pprint(collect['res'])

 

 

posted on 2019-01-11 10:09  hailuo  阅读(815)  评论(0编辑  收藏  举报