Python 装饰器实现的retry 出错重试

2015-01-21 0 190
Python 装饰器实现的retry 出错重试
# 可以用来处理HTTP超时,等需要重试的错误方法

import requests

def retry(attempt):
    def decorator(func):
        def wrapper(*args, **kw):
            att = 0
            while att < attempt:
                try:
                    return func(*args, **kw)
                except Exception as e:
                    att += 1
        return wrapper
    return decorator

# 重试次数
@rety(attempt=3)
def get_response(url):
    r = requests.get('http://www.oschina.net')
    return r.content

遇见资源网 python Python 装饰器实现的retry 出错重试 http://www.ox520.com/15072.html

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务