6

aiohttp用来请求网址。大多数时候它运行正常,但有时它会停止而不会引发任何异常。

正如您在代码中看到的,我捕获了所有异常,但是当它停止时,不会打印任何异常日志。

日志如下所示:

get_live_league_games: while True
try
yield from aiohttp.request

但 ' res = yield from r.json()' 不打印,它停止并且不抛出任何异常。

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)
4

1 回答 1

5

可能是由于互联网性质而发生的——连接可能会在出现断开连接错误之前“挂起”很长时间。

这就是为什么您通常需要客户端 http 操作超时。

我建议将 call 包装aiohttp.request()asyncio.wait_for.

于 2015-08-13T02:53:14.433 回答