6
在我utility.py我有,爲什麼Contextmanager拋出一個運行時錯誤'生成器在throw()'後沒有停止?
@contextmanager
def rate_limit_protection(max_tries=3, wait=300):
tries = 0
while max_tries > tries:
try:
yield
break
except FacebookRequestError as e:
pprint.pprint(e)
if e._body['error']['message'] == '(#17) User request limit reached':
print("waiting...")
time.sleep(wait)
tries += 1
在我task.py我打電話:
for date in interval:
with utility.rate_limit_protection():
stats = account.get_insights(params=params)
乳寧任務給定日期範圍後,一旦Facebook的限速踢的程序等待300秒,然後失敗並顯示錯誤。
File "/Users/kamal/.pyenv/versions/3.4.0/lib/python3.4/contextlib.py", line 78, in __exit__
raise RuntimeError("generator didn't stop")
RuntimeError: generator didn't stop
您是否期待這個'rate_limit_protection'上下文管理器重複運行'with'語句的主體? '與'不這樣做。身體運行一次。 – user2357112
否否..圍繞with語句有for循環。 with語句爲for循環的整個主體運行一次。你的意思是我的rate_limit_protection()函數中沒有while循環 –
爲什麼你會期望with語句在整個循環中運行一次,如果它在循環體內呢? – user2357112