python – 线程类中__init__的相反
发布时间:2021-03-30 20:45:52 所属栏目:Python 来源:互联网
导读:我知道当你创建像newThread = MyThread(property)这样的类并且newthread.start()触发run()时会自动调用__init __().我正在寻找的是在线程终止之前自动调用的东西,所以我不必在每个return语句之前显式调用self.cleanUp(). class MyThread(Thread): def __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(),因为线程未成功运行 – 尽管您可以根据需要更改它,但可能还需要确保它不会被调用两次.以下是上述代码在这种情况下的作用: 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 – Django的call_command失败,缺少必需的参数
- Python:ndarray.flatten(‘F’)有否相反?
- 在测试python max recursion depth时,为什么我多次遇到Runt
- Python中map和列表推导效率比较实例分析
- wpf – Dispatcher与后台线程的区别?
- python源代码中的sys模块在哪里?
- python – 为什么time.clock比time.time给出更长的时间?
- 如何使numba @jit使用所有cpu核心(parallelize numba @jit)
- python中的全局变量混淆
- 有没有办法在python中的特定索引附加/扩展列表与另一个列表
推荐文章
站长推荐
热点阅读
