Last active
October 11, 2017 09:58
-
-
Save StarsHu/ce12236e216bf493aa0a720a711dd4d1 to your computer and use it in GitHub Desktop.
python_decorator_bug
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
from functools import wraps | |
def retry(retry_time=10): | |
def retry_decorator(func): | |
@wraps(func) | |
def wrapper(*args, **kwargs): | |
while True: | |
result = func(*args, **kwargs) | |
if result is None: | |
if retry_time <= 0: | |
raise Exception('错误, 无法获得数据') | |
time.sleep(0.5) | |
retry_time -= 1 | |
else: | |
return result | |
return wrapper | |
return retry_decorator | |
@retry(5) | |
def test(a, b=1): | |
print(a, b) | |
return None | |
if __name__ == '__main__': | |
test(4) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
from functools import wraps | |
def retry(retry_time=10): | |
def retry_decorator(func): | |
@wraps(func) | |
def wrapper(*args, **kwargs): | |
for _ in range(retry_time): | |
result = func(*args, **kwargs) | |
if result: | |
break | |
time.sleep(0.5) | |
else: | |
raise Exception('错误, 无法获得数据') | |
return result | |
return wrapper | |
return retry_decorator | |
@retry(5) | |
def test(a, b=1): | |
print(a, b) | |
return None | |
if __name__ == '__main__': | |
test(4) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
from functools import wraps | |
def retry(retry_time=10): | |
def retry_decorator(func): | |
@wraps(func) | |
def wrapper(*args, **kwargs): | |
while retry_count: | |
result = func(*args, **kwargs) | |
if result: | |
break | |
time.sleep(0.5) | |
retry_count -= 1 | |
else: | |
raise Exception('远程服务器错误, 无法获得数据') | |
return result | |
return wrapper | |
return retry_decorator | |
@retry(5) | |
def test(a, b=1): | |
print(a, b) | |
return None | |
if __name__ == '__main__': | |
test(4) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
加上
nonlocal retry_time
就行了