2015-08-12 33 views
5

Tôi sử dụng aiohttp để yêu cầu url. Hầu hết thời gian nó chạy bình thường, nhưng đôi khi nó dừng lại mà không gây bất kỳ ngoại lệ nào.Yêu cầu aiohttp của Python đã dừng nhưng không có ngoại lệ

Như bạn có thể thấy trong mã, tôi bắt tất cả ngoại lệ, nhưng khi không dừng nhật ký ngoại lệ được in.

Các bản ghi như sau:

get_live_league_games: while True 
try 
yield from aiohttp.request 

nhưng 'res = yield from r.json()' không in, nó dừng lại và không ném bất kỳ trường hợp ngoại lệ.

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) 
+0

không liên quan: mã rất lặp đi lặp lại. Bạn có thể thả tất cả các trình xử lý ngoại lệ ngoại trừ trình xử lý cuối cùng. – jfs

Trả lời

5

Đó thể xảy ra thiên nhiên internet do - kết nối có thể 'treo' cho thời gian rất dài trước khi lỗi ngắt kết nối tăng.

Đó là lý do tại sao bạn thường cần thời gian chờ cho các hoạt động http của khách hàng.

Tôi đề nghị gói aiohttp.request() gọi vào asyncio.wait_for.

+0

Thông số thời gian chờ của http có hoạt động không? sẽ aiohttp ném ngoại lệ timeout? – Nianing

+0

Dường như mã dừng ở 'yield from r.json() ' – Nianing

+0

' yield từ r.json() 'thực sự đọc cơ thể tải trọng HTTP vì vậy đây là điểm có khả năng nhất để treo –

Các vấn đề liên quan