asyncio Lock,Queue

 

#
# 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 = [] #If you don't need to limit the current 

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()]

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324813482&siteId=291194637