Python上下文管理器如何尝试执行代码?

编程入门 行业动态 更新时间:2024-10-23 17:36:41
本文介绍了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上下文管理器如何尝试执行代码?

本文发布于:2023-11-23 10:06:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1621050.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:上下文   管理器   代码   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!