1

通过两种不同的实现,我得到了相当不同的结果。

这是实现1

request_semaphore = asyncio.Semaphore(5)
async def _send_async_request(client: AsyncClient, method, auth, url, body):
  async with request_semaphore:
    try:
      async for attempt in AsyncRetrying(stop=stop_after_attempt(3), wait=wait_fixed(1)):
        with attempt:    
          response = await client.request(method=method, url=url, auth=auth, json=body)
          response.raise_for_status()
          return response
    except RetryError as e:
      pass

这是实现2:

request_semaphore = asyncio.Semaphore(5)
@retry(stop=stop_after_attempt(3), wait=wait_fixed(1))
async def _send_single_async_request(self, client: AsyncClient, method, auth, url, body):
  async with request_semaphore:
    response = await client.request(method=method, url=url, auth=auth, json=body)
    response.raise_for_status()
    return response

async def _send_async_request(self, client: AsyncClient, method, auth, url, body):
  try:
    await self._send_single_async_request(client, method, auth, request)
  except RetryError as e:
    pass

我正在针对稳定的 REST API 对其进行测试。以下是基准:

  1. 100 个成功的 POST 请求:
  • 实施 1:0:59 分钟
  • 实施 2:0:57 分钟
  1. 100 个失败的 POST 请求:
  • 实施 1:3:26 分钟
  • 实施 2:2:09 分钟

这些结果是一致的。谁能帮我理解为什么我的第一个实现比我的第二个慢?

编辑:仅供参考,这就是我调用上述函数的方式(上述函数实际上接收带有 url 和正文的请求元组,为清楚起见对其进行了编辑)

async def _prepare_async_requests(method, auth, requests):
    async with AsyncClient() as client:
      task_list = [self._send_async_request(client, method, auth, request) for request in requests]
      return [await task for task in asyncio.as_completed(task_list)]

def send_async_requests(auth, method, requests):
    loop = asyncio.get_event_loop()
    responses = loop.run_until_complete(self._prepare_async_requests(method, auth, requests))
    return responses
4

0 回答 0