Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)
like image 462
Nianing Avatar asked Aug 12 '15 16:08

Nianing


1 Answers

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.

like image 125
Andrew Svetlov Avatar answered Oct 04 '22 22:10

Andrew Svetlov