Python aiohttp request stopped but threw no exceptions

I am using aiohttp to request a url. Most of the time it works fine, but sometimes it stops without any exceptions.

As you can see in the code, I catch all exceptions, but when it stops, the exception log is not printed.

Logs look like this:

 get_live_league_games: while True try yield from aiohttp.request 

but ' res = yield from r.json() ' does not print, it stops and does not throw any exceptions.

 while True: print('get_live_league_games: while True') start = time.clock() try: print('try') r = yield from aiohttp.request('GET',url) print('yield from aiohttp.request') res = yield from r.json() print('res = yield from r.json()') except aiohttp.errors.DisconnectedError as e: logging.warning('get_live_league_games:',e) yield from asyncio.sleep(10) continue except aiohttp.errors.ClientError as e: logging.warning('get_live_league_games:',e) yield from asyncio.sleep(10) continue except aiohttp.errors.HttpProcessingError as e: logging.warning('get_live_league_games:',e) yield from asyncio.sleep(10) continue except Exception as e: logging.warning('get_live_league_games,Exception:',e) yield from asyncio.sleep(10) continue print('request internet time : ', time.clock()-start) yield from asyncio.sleep(10) 
+5
source share
1 answer

This may be due to the Internet nature - the connection may freeze for a very long time before a disconnect error occurs.

For this, you usually need a timeout for http client operations.

I suggest wrapping the aiohttp.request() call in asyncio.wait_for .

+5
source

All Articles