From 0685338d6dcc59b3b9922c39f8d080c20647ee6f Mon Sep 17 00:00:00 2001 From: Jonathan Stray Date: Thu, 24 Oct 2024 17:43:46 -0700 Subject: [PATCH] Move await response.json() inside timeout retry --- perspective_ranker.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/perspective_ranker.py b/perspective_ranker.py index 33d884a..add59ac 100644 --- a/perspective_ranker.py +++ b/perspective_ranker.py @@ -220,11 +220,14 @@ async def score(self, attributes, statement, statement_id): try: start_time = time.time() + response_json = None for _ in range(0,3): try: response = await self.client.post( url=PERSPECTIVE_URL, json=data, headers=headers, timeout=self.scoring_timeout ) + response.raise_for_status() + response_json = await response.json() except asyncio.TimeoutError: scoring_timeouts.inc() logger.warning( @@ -235,12 +238,9 @@ async def score(self, attributes, statement, statement_id): latency = time.time() - start_time scoring_latency.observe(latency) - if not response: + if not response_json: raise HTTPException(status_code=status.HTTP_504_GATEWAY_TIMEOUT, detail=f"Gave up after 3 timeouts while scoring statement_id {statement_id}") - response.raise_for_status() - response_json = await response.json() - results = [] scorable = True for attr in attributes: