Nianing
Nianing

Reputation: 73

Python aiohttp request stopped but raised no exception

I use aiohttp to request the url. Most of the time it runs normally, but sometimes it stops without raising any exception.

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

The logs look like:

get_live_league_games: while True
try
yield from aiohttp.request

but the '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)

Upvotes: 7

Views: 2555

Answers (1)

Andrew Svetlov
Andrew Svetlov

Reputation: 17356

That may happen due internet nature -- connection may 'hang' for very long period before disconnection error raises.

That's why you usually need timeout for client http operations.

I suggest wrapping aiohttp.request() call into asyncio.wait_for.

Upvotes: 5

Related Questions