python3 异步并发

python3 异步并发

1. TCPConnector 链接池

import asyncio
from aiohttp import ClientSession, TCPConnector
async def aiohttp_get():
    url = 'url'
    conn = TCPConnector(limit=10)  # 限制同时链接数,连接默认是100,limit=0 无限制
    async with ClientSession(connector=conn) as session:
      async with session.get(url) as response:
              html = await response.text()
              return html

2. Semaphore 信号量

from aiohttp import ClientSession 
import asyncio
 
#  限制协程并发量
async def hello(sem, url):
 
    async with sem:
        async with ClientSession() as session:
            async with session.get(f'http://localhost:8080/{url}') as response:
                r = await response.read()
                print(r)
                await asyncio.sleep(1)
 
 
def main():
    loop = asyncio.get_event_loop()
    tasks = []
    sem = asyncio.Semaphore(5)  # this 
    for i in range(100000):
        task = asyncio.ensure_future(hello(sem, i))
        tasks.append(task)
    
    feature = asyncio.ensure_future(asyncio.gather(*tasks))
    loop.run_until_complete(feature)
 
if __name__ == "__main__":
    main()

https://blog.csdn.net/joson1234567890/article/details/105762195
https://cloud.tencent.com/developer/article/1787184
https://blog.csdn.net/whatday/article/details/106886717
https://www.liujiangblog.com/course/python/83
https://python-parallel-programmning-cookbook.readthedocs.io/zh_CN/latest/chapter4/05_Task_manipulation_with_Asyncio.html

posted @ 2023-03-21 17:42  michaelchengjl  阅读(27)  评论(0编辑  收藏  举报