问题描述
我正在使用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 (将#修改为@)