为什么Contextmanager抛出运行时错误“generator did not stop after throw()”?

2024-05-23 14:36:29 发布

您现在位置:Python中文网/ 问答频道 /正文

在我的实用程序里

@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

Tags: inpy实用程序rateparamsgeneratormaxpprint