日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java 限制并发数_限制并发请求数aiohttp

發布時間:2023/12/2 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 限制并发数_限制并发请求数aiohttp 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

您的限制設置正常 . 你在調試時弄錯了 .

正如Mikhail Gerasimov在the comment指出的那樣,你將 print() 調用放在錯誤的位置 - 它必須在 session.get() 上下文中 .

為了確保限制得到尊重,我針對簡單的日志記錄服務器測試了您的代碼 - 測試顯示服務器接收到您在 TCPConnector 中設置的確切數量的連接 . 這是測試:

import asyncio

import aiohttp

loop = asyncio.get_event_loop()

class SilentServer(asyncio.Protocol):

def connection_made(self, transport):

# We will know when the connection is actually made:

print('SERVER |', transport.get_extra_info('peername'))

async def get_images(url, session):

chunk_size = 100

# This log doesn't guarantee that we will connect,

# session.get() will freeze if you reach TCPConnector limit

print(f'CLIENT | Making request to {url}')

async with session.get(url=url) as r:

while True:

chunk = await r.content.read(chunk_size)

if not chunk:

break

urls = [f'http://127.0.0.1:1337/{x}' for x in range(20)]

conn = aiohttp.TCPConnector(limit=3)

session = aiohttp.ClientSession(connector=conn, loop=loop)

async def test():

await loop.create_server(SilentServer, '127.0.0.1', 1337)

await asyncio.gather(*(get_images(url, session=session) for url in urls))

loop.run_until_complete(test())

總結

以上是生活随笔為你收集整理的java 限制并发数_限制并发请求数aiohttp的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。