aiohttp: set maximum number of requests per second

前端 未结 4 1047
耶瑟儿~
耶瑟儿~ 2020-12-05 07:11

How can I set maximum number of requests per second (limit them) in client side using aiohttp?

4条回答
  •  爱一瞬间的悲伤
    2020-12-05 07:50

    Since v2.0, when using a ClientSession, aiohttp automatically limits the number of simultaneous connections to 100.

    You can modify the limit by creating your own TCPConnector and passing it into the ClientSession. For instance, to create a client limited to 50 simultaneous requests:

    import aiohttp
    
    connector = aiohttp.TCPConnector(limit=50)
    client = aiohttp.ClientSession(connector=connector)
    

    In case it's better suited to your use case, there is also a limit_per_host parameter (which is off by default) that you can pass to limit the number of simultaneous connections to the same "endpoint". Per the docs:

    limit_per_host (int) – limit for simultaneous connections to the same endpoint. Endpoints are the same if they are have equal (host, port, is_ssl) triple.

    Example usage:

    import aiohttp
    
    connector = aiohttp.TCPConnector(limit_per_host=50)
    client = aiohttp.ClientSession(connector=connector)
    

提交回复
热议问题