self: 限制并发量asyncio
#coding:utf-8 import time,asyncio a=time.time() id=1 async def hello(id,semaphore): async with semaphore: await asyncio.sleep(1) print('working id:'+str(id)) async def run(): semaphore = asyncio.Semaphore(5) # 限制并发量为5 to_get = [hello(id,semaphore) for id in range(20)] #总共20任务 await asyncio.wait(to_get) if __name__ == '__main__': loop = asyncio.get_event_loop() loop.run_until_complete(run()) loop.close() print(time.time()-a)