在thread类中与\uuInit_Uu相反

2024-10-01 15:33:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我知道当您创建一个类似于newThread = MyThread(property)的类时,{init_uuUi()是自动调用的,run()是由newthread.start()触发的。我要寻找的是在线程终止之前自动调用的东西,所以我不必在每个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

Tags: runselfreturninitdefproperty语句thread
3条回答

如果您试图解决的问题是您不想在每个run()方法中添加代码来调用清理函数,那么我建议您创建一个自定义的Thread子类,它可以为您实现这一点。可能是这样的:

class CleanupThread(Thread):
    def cleanup(self):
        # Override this method in your subclasses to do cleanup
        pass

    def run2(self):
        # Override this method in your subclasses instead of run()
        pass

    def run(self):
        # Do *not* override this in your subclasses. Override run2() instead.
        try:
            self.run2()
        finally:
            self.cleanup()

当然,您可以将run2重命名为对您有意义的名称。在

Python并没有提供与此类似的内置功能,如果这正是您想要的。在

Python mailing list中所述,__del__不应被视为相反的,但是您可以使用with语法,它是context manager

you cannot be sure that an object's destructor (__del__() ) will ever be called. If you want to make sure that a particular object gets processed, one approach is the with- syntax.

或者您也可以查看try…finally子句,其中finally语句将始终运行。在

class MyThread(Thread): 

    def __init__(self, property): 
        Thread.__init__(self)
        self.property = property

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_value, traceback):
        print('starting cleanup')
        # Clean up here

    def run(self):
        # Do some stuff
        return

# not now you can call it like this:
with MyThread("spam") as spam:
    print("The thread is running")
    # you can also do stuff here

可以使用try…finally子句,如下所示:

^{pr2}$

一种方法是使Thread子类也是context manager。这将有效地使__exit__()成为您想要触发的特殊方法。在

下面是我的提议。注意:我重命名了传递给构造函数的property参数,因为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...')

输出:

^{pr2}$

如果您将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!

相关问题 更多 >

    热门问题