python C 线程类中__init__的相反
我知道当你创建像newThread = MyThread(property)这样的类并且newthread.start()触发run()时会自动调用__init __().我正在寻找的是在线程终止之前自动调用的东西,所以我不必在每个return语句之前显式调用self.cleanUp(). class MyThread(Thread): def __init__(self,property): Thread.__init__(self) self.property = property def cleanUp(self): # Clean up here def run(self): # Do some stuff self.cleanUp() # Current work around return 解决方法 一种方法是使Thread子类也为 context manager.这将有效地使__exit __()成为您想要触发的特殊方法. 以下显示了我的建议.注意:我重命名了您传递构造函数的属性参数,因为property是Python内置的名称. from threading import Thread import time TEST_THREAD_EXCEPTION = False # change as desired class MyThread(Thread): def __init__(self,attribute): Thread.__init__(self) self.attribute = attribute def cleanup(self): # Clean up here print(' cleaning up after thread') def run(self): if TEST_THREAD_EXCEPTION: raise RuntimeError('OOPS!') # force exception print(' other thread now running...') time.sleep(2) # Do something... def __enter__(self): try: self.run() except Exception as exc: print('Error: {} exception raised by thread'.format(exc)) raise # reraise the exception return self def __exit__(self,*args): self.cleanup() print('main thread begins execution') with MyThread('hello') as thread: print('doing other things in main thread while other thread is running') print('main thread continuing...')输出: main thread begins execution other thread now running... doing other things in main thread while other thread is running cleaning up after thread main thread continuing on...如果将TEST_THREAD_EXCEPTION更改为True,则不会调用cleanup(),因为线程未成功运行 C 尽管您可以根据需要更改它,但可能还需要确保它不会被调用两次.以下是上述代码在这种情况下的作用: main thread begins execution Error: OOPS! exception raised by thread Traceback (most recent call last): File "opposite_init.py",line 37,in <module> with MyThread('hello') as thread: File "opposite_init.py",line 27,in __enter__ self.run() File "opposite_init.py",line 21,in run raise RuntimeError('OOPS!') # force exception RuntimeError: OOPS!(编辑:甘南站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- 将Python虚拟环境与终结器配合使用
- python – J的x型变量:它们如何在内部存储?
- python – 近似大对称矩阵的最高3个特征值和特征向量的快速
- python – numpy – 将非连续数据转换为适当的连续数据
- django-forms – 如何使用modelformset_factory创建一个空的
- python-2.7 – 在Python 2.7中手动构建ConfigParser的深层副
- python – django中的高效分页和数据库查询
- python-2.7 – OpenCV:libdc1394错误:无法初始化libdc139
- python – 芹菜工作者无法连接到docker实例上的redis
- 具有上下文变量参数的Django自定义模板标记