python 修饰器 retry

#!/usr/bin/env python2
#-*- coding: utf-8 -*-

def retry(attempt):
  def decorator(func):
    def wrapper(*args, **kw):
      print args, kw
      att = 0
      while att < attempt:
        func(*args, **kw)
        att += 1
    return wrapper
  return decorator

# 重试次数
@retry(attempt = 3)
def test(s, x = 1):
  print s

if __name__ == "__main__":
  test('----', x = 2)

posted on 2015-12-18 10:24  DayAfterDay  阅读(444)  评论(0编辑  收藏  举报

导航