微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我的代码无法阻止asyncio.TimeoutError并继续循环

如何解决我的代码无法阻止asyncio.TimeoutError并继续循环

我正在使用aiohttp,但是代码大多数时候都停止了,并返回TimeoutError。我尝试了几件事,但仍然无法防止引发asyncio.TimeoutError。我的代码asyncio肯定有问题。永远不会引发TimeoutError

def get_responses(self,urls,office_token,params = None):
            
            loop = asyncio.get_event_loop()
            future = asyncio.ensure_future(self.run(office_token,params))
            responses = loop.run_until_complete(future)
            return responses
    
        @backoff.on_exception(backoff.expo,asyncio.TimeoutError,max_time=500)
        async def fetch(self,url,session,params):
         try:
          async with session.get(url,params=params) as response:
            Now = int(time.time())
            print(response)
            output = await response.read()
            return output
    
         except asyncio.TimeoutError as e:
            log(self.args,str(e))
    
    
        async def bound_fetch(self,sem,params):
        # Getter function with semaphore.
            async with sem:
              output = await self.fetch(url,params)
            return {"url": url,"output":output}
    
        async def run(self,params):
            tasks = []
            # create instance of Semaphore
            sem = asyncio.Semaphore(500)
            timeout = ClientTimeout(total=1000)
    
            async with ClientSession(auth=BasicAuth(office_token,password=' '),timeout = timeout,connector=TCPConnector(ssl=False)) as session:
    
                for url in urls:
                    # pass Semaphore and session to every GET request
                    task = asyncio.ensure_future(self.bound_fetch(sem,params))
                    tasks.append(task)
    
                responses = await asyncio.gather(*tasks)
                return responses

错误是:

async with session.get(url,params=params) as response:
  File "/usr/local/lib64/python3.7/site-packages/aiohttp/client.py",line 1012,in __aenter__
    self._resp = await self._coro
  File "/usr/local/lib64/python3.7/site-packages/aiohttp/client.py",line 504,in _request
    await resp.start(conn)
  File "/usr/local/lib64/python3.7/site-packages/aiohttp/client_reqrep.py",line 860,in start
    self._continue = None
  File "/usr/local/lib64/python3.7/site-packages/aiohttp/helpers.py",line 596,in __exit__
    raise asyncio.TimeoutError from None
concurrent.futures._base.TimeoutError

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。