retrying是一个很好用的关于重试的Python包,可以用来自动重试一些可能会运行失败的程序段。
<h2>为什么选择<em>retrying <p dir="auto">我们在写一些程序的时候,会去调用一些外部资源、组件,这些外部程序对我们来说是不可控的,所以它们不可用、运行失败都是正常的事情,尤其是在网络上多个服务交互的时候,在写程序的时候一定要考虑调用外部程序会失败的情况。 <h2>retrying <p dir="auto"><em>retrying就是一个可以用来应对上述问题的工具,可以在<a href="https://pypi.python.org/pypi/retrying" target="_blank" rel="nofollow noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">PyPi上获得。<br /> <em>retrying提供一个装饰器函数<code>retry,被装饰的函数就会在运行失败的条件下重新执行,默认只要一直报错就会不断重试,看一下下面的例子: <pre><code>import random from retrying import retry @retry def have_a_try(): if random.randint(0, 10) != 5: raise Exception('It's not 5!') print 'It's 5!' <p dir="auto">如果我们运行<code>have_a_try函数,那么直到<code>random.randint返回<code>5,它才会执行结束,否则会一直重新执行。<br /> <em>retry还可以接受一些参数,这个从源码中Retrying类的初始化函数可以看到可选的参数: <pre><code> def __init__(self, stop=None, wait=None, stop_max_attempt_number=None, stop_max_delay=None, wait_fixed=None, wait_random_min=None, wait_random_max=None, wait_incrementing_start=None, wait_incrementing_increment=None, wait_exponential_multiplier=None, wait_exponential_max=None, retry_on_exception=None, retry_on_result=None, wrap_exception=False, stop_func=None, wait_func=None, wait_jitter_max=None) <ul> <li><code>stop_max_attempt_number:用来设定最大的尝试次数,超过该次数就停止重试 <li><code>stop_max_delay:比如设置成10000,那么从被装饰的函数开始执行的时间点开始,到函数成功运行结束或者失败报错中止的时间点,只要这段时间超过10秒,函数就不会再执行了 <li><code>wait_fixed:设置在两次retrying之间的停留时间 <li><code>wait_random_min和<code>wait_random_max:用随机的方式产生两次retrying之间的停留时间 <li><code>wait_exponential_multiplier和<code>wait_exponential_max:以指数的形式产生两次retrying之间的停留时间,产生的值为<strong>2^previous_attempt_number * wait_exponential_multiplier,previous_attempt_number是前面已经retry的次数,如果产生的这个值超过了wait_exponential_max的大小,那么之后两个retrying之间的停留值都为wait_exponential_max。这个设计迎合了<a href="https://julien.danjou.info/blog/2015/python-retrying" target="_blank" rel="nofollow noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">exponential backoff算法,可以减轻阻塞的情况。 <li>我们可以指定要在出现哪些异常的时候再去retry,这个要用<code>retry_on_exception传入一个函数对象: <pre><code>def retry_if_io_error(exception): return isinstance(exception, IOError) @retry(retry_on_exception=retry_if_io_error) def read_a_file(): with open("file", "r") as f: return f.read() <p dir="auto">在执行<code>read_a_file函数的过程中,如果报出异常,那么这个异常会以形参<code>exception传入<code>retry_if_io_error函数中,如果<code>exception是<code>IOError那么就进行retry,如果不是就停止运行并抛出异常。 <ul> <li>我们还可以指定要在得到哪些结果的时候去retry,这个要用<code>retry_on_result传入一个函数对象: <pre><code>def retry_if_result_none(result): return result is None @retry(retry_on_result=retry_if_result_none) def get_result(): return None <p dir="auto">在执行get_result成功后,会将函数的返回值通过形参<code>result的形式传入<code>retry_if_result_none函数中,如果返回值是<code>None那么就进行retry,否则就结束并返回函数值。 <h2>参考资料 <ul> <li><a href="https://julien.danjou.info/blog/2015/python-retrying" target="_blank" rel="nofollow noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Get back up and try again: retrying in Python <li><a href="https://github.com/rholder/retrying" target="_blank" rel="nofollow noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">retrying
Hi! I am a robot. I just upvoted you! I found similar content that readers might be interested in:
http://weixin.niurenqushi.com/article/2017-05-23/4859813.html
Congratulations @heha37! You have completed some achievement on Steemit and have been rewarded with new badge(s) :
<p dir="auto"><a href="http://steemitboard.com/@heha37" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link"><img src="https://images.hive.blog/768x0/https://steemitimages.com/70x80/http://steemitboard.com/notifications/firstpost.png" srcset="https://images.hive.blog/768x0/https://steemitimages.com/70x80/http://steemitboard.com/notifications/firstpost.png 1x, https://images.hive.blog/1536x0/https://steemitimages.com/70x80/http://steemitboard.com/notifications/firstpost.png 2x" /> You published your First Post<br /> <a href="http://steemitboard.com/@heha37" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link"><img src="https://images.hive.blog/768x0/https://steemitimages.com/70x80/http://steemitboard.com/notifications/firstvoted.png" srcset="https://images.hive.blog/768x0/https://steemitimages.com/70x80/http://steemitboard.com/notifications/firstvoted.png 1x, https://images.hive.blog/1536x0/https://steemitimages.com/70x80/http://steemitboard.com/notifications/firstvoted.png 2x" /> You got a First Vote <p dir="auto">Click on any badge to view your own Board of Honnor on SteemitBoard.<br /> For more information about SteemitBoard, click <a href="https://steemit.com/@steemitboard" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">here <p dir="auto">If you no longer want to receive notifications, reply to this comment with the word <code>STOP <p dir="auto">If you want to support the SteemitBoard project, your upvote for this notification is welcome!