加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – 线程类中__init__的相反

发布时间:2020-12-16 23:06:02 所属栏目: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!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读