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)

  


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM