# # total = 0 # # async def add(): # #1. dosomething1 # #2. io操做 # # 1. dosomething3 # global total # for i in range(1000000): # total += 1 # async def desc(): # global total # for i in range(1000000): # total -= 1 # # if __name__ == "__main__": # import asyncio # tasks = [add(),desc()] # loop = asyncio.get_event_loop() # loop.run_until_complete(asyncio.wait(tasks)) # print(total) import asyncio import aiohttp from asyncio import Lock,Queue cache = {} lock = Lock() queue = Queue() # await queue.get() queue = []# 若是不用限流 async def get_stuff(url): async with lock: if url in cache: return cache[url] stuff = await aiohttp.request('GET',url) cache[url] = stuff return stuff async def parse_stuff(): stuff = await get_stuff() #do some parsing async def use_stuff(): stuff = await get_stuff() #use some parsing tasks = [parse_stuff(),use_stuff()]