Python上下文管理器如何尝试执行代码? [英] How can a Python context manager try to execute code?

查看:192
本文介绍了Python上下文管理器如何尝试执行代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个小的上下文管理器,尝试重复执行一些代码,直到代码工作或直到进行了一定数量的尝试。我试图写这个,但遇到一个困难,让上下文管理器处理问题时产生:

I'm trying to write a small context manager that'll try to execute some code repeatedly until the code works or until a specified number of tries has been made. I have attempted to write this but am encountering a difficulty with having the context manager handle problems when yielding:

Exception RuntimeError: 'generator ignored GeneratorExit'

我应该如何编写这个?

import contextlib
import random

def main():

    with nolube():
        print(1 / random.randint(0, 1))

@contextlib.contextmanager
def nolube(
    tries = None # None: try indefinitely
    ):
    """
    Create a context for trying something repeatedly.
    """
    tries_done = 0
    rekt = True
    if tries is None:
        while rekt is True:
            try:
                yield
                rekt = False
            except:
                tries_done += 1
                pass
    else:
        while rekt is True and tries_done <= tries:
            try:
                yield
                rekt = False
            except:
                tries_done += 1
                pass

if __name__ == "__main__":
    main()


推荐答案

@ contextlib.contextmanager 有一个非常明确的合同; 只能恢复。它不能用于重新运行代码。

@contextlib.contextmanager has a very clear contract; it'll only be resumed once. It can't be used to re-run code.

事实上,您不能使用上下文管理器来控制重复 。你需要一个循环,而不是上下文管理器。上下文管理器不控制块,只有在进入和退出时才被通知。

In fact, you can't use a context manager to control repetitions at all. You need a loop here, not a context manager. A context manager doesn't control the block, it is only informed when entering and exiting.

使用 重试;它提供了一个装饰器。装饰器中包装一个功能,而True 循环,将为您重新运行该功能。

Use the retrying package instead; it provides a decorator. The decorator wraps a function in a while True loop that'll re-run the function for you.

您可以将其应用于您的案例,方法是移动 print()语句转换成一个函数,用 @retry 装饰,然后调用该函数:

You'd apply it to your case by moving the print() statement into a function, decorated with @retry, then calling that function:

import random
from retrying import retry

@retry
def foo():
    print(1 / random.randint(0, 1))

def main():
    foo()

这篇关于Python上下文管理器如何尝试执行代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆