简体   繁体   English

带有 Python 重试装饰器的动态参数

[英]Dynamic parameters with Python's retry decorator

I'm currently using the python retrying package which contains the @retry decorator that has a number of optional parameters.我目前正在使用python 重试包,其中包含具有许多可选参数的@retry装饰器。 I have these parameters set correctly for our production environment with long enough wait times in between retries (below it's set to 2000 milliseconds) but I would like to set these values differently for unit testing purposes so that execution is very quick.我为我们的生产环境正确设置了这些参数,重试之间的等待时间足够长(低于 2000 毫秒),但我想为单元测试目的设置不同的这些值,以便执行速度非常快。

For example here the wait_fixed time is set to 2000 milliseconds for production but for my unit test that calls some_function() I'd like to override the wait_fixed parameter to be 1 millisecond so it executes very quickly.例如,这里的 wait_fixed 时间设置为 2000 毫秒以用于生产,但对于调用some_function()单元测试,我想将wait_fixed参数覆盖为 1 毫秒,以便它执行得非常快。

@retry(stop_max_attempt_number=3, wait_fixed=2000)
def some_function(self):
    return True

The problem I'm running into is that the decorator is interpreted when the function is defined so as of yet I have not found a way to override the wait_fixed parameter from my unit tests.遇到的问题是装饰器在定义函数时解释,到目前为止我还没有找到一种方法来覆盖我的单元测试中的wait_fixed参数。

main.py

import settings
from retrying import retry


@retry(stop_max_attempt_number=settings.STOP_MAX_ATTEMPT_NUMBER, 
       wait_fixed=settings.WAIT_FIXED)
def some_function(self):
    return True

settings.py

import json


with open('config.json') as jsonf:
    config = json.loads(jsonf.read())

WAIT_FIXED=int(config['WAIT_FIXED'])
STOP_MAX_ATTEMPT_NUMBER=int(config['STOP_MAX_ATTEMPT_NUMBER'])

config.json

{
    "STOP_MAX_ATTEMPT_NUMBER" : "3",
    "WAIT_FIXED" : "2000"
}

Have your test runner put a suitable config.json in place.让你的测试运行器放置一个合适的config.json到位。

I recently come across a problem which having a def inside a def resolved the issue.我最近遇到了一个问题,在def有一个def解决了这个问题。


I need to use the retrying decorator on get_url function but need to pass configurable value for stop_max_attempt_number .我需要在get_url函数上使用重试装饰器,但需要为stop_max_attempt_number传递可配置值。 There is no way I can come up to have get_url , along with other functions at the same indent.我无法想出get_url以及相同缩进的其他函数。

To solve the issue, I have the get_url function defined inside get_analytic function.为了解决这个问题,我有get_url里面定义的函数get_analytic功能。 For illustration:举例说明:

def get_analytics(conf: Configuration, assets: list, cache: dict) -> list:
    STOP_MAX_ATTEMPT_NUMBER = int(conf.num_retry)
    WAIT_FIXED = int(conf.retry_timeout)
    @retry(stop_max_attempt_number=STOP_MAX_ATTEMPT_NUMBER, wait_fixed=WAIT_FIXED)
    def get_url(disable_https: bool, url: str, user: str, password: str) -> Response:

I needed to be able to set the retry parameters dynamically when the function is called, the below worked well for me:我需要能够在调用函数时动态设置重试参数,以下对我来说效果很好:

import random
from retrying import retry


class MyClass(object):

    def try_stuff(self, string, attempts, wait):
        self.do_something_with_retry = retry(stop_max_attempt_number=attempts,
                                             wait_fixed=wait)(self.do_something_unreliable)
        return self.do_something_with_retry(string)

    def do_something_unreliable(self, string):
        print(string)
        if random.randint(0, 10) > 1:
            raise IOError("Broken sauce, everything is hosed!!!111one")
        else:
            return "Awesome sauce!"

instance = MyClass()

print instance.try_stuff('test', 10, 2000)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM