Python/PySide:如何销毁终止的线程对象? [英] Python/PySide: How can i destroy a terminated thread object?

查看:148
本文介绍了Python/PySide:如何销毁终止的线程对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一个按钮来停止一个带有进程的线程,它可以工作,但不像预期的那样:我无法删除线程对象.(似乎删除了对线程对象的引用,但删除线程对象不会自动断开信号,无论如何我都可以通过信号访问它.)

I would like to implement a button to stop a thread with a process, it works but not as expected: i can't delete the thread object. ( The reference to the thread object seems to be deleted, but the signals are not disconnected automatically by deleting the thread object, i can access it anyway via the signal.)

我有一个带有 thread_worker 类的模块和一个作为进程运行的复杂处理函数:

I have a modul with a class thread_worker and a function for complex processing which is running as process:

from PySide.QtCore import *
from PySide.QtGui import *
import multiprocessing as mp
import time

# this function runs as a process
def complex_processing(queue):
    # do something
    ...

class thread_worker(QThread):
    message_signal = Signal(str)
    stop_thread_signal = Signal()

    def __init__(self, prozessID, sleepTime, parent=None):
        super(ThreadProzessWorker, self).__init__(parent)
        self.queue = mp.Queue()
        self.process = mp.Process(target=complex_processing, args=(self.queue,))
        self.timeStamp = int(time.time())

    def run(self):
        self.process.start()
        self.process.join()

    @Slot()
    def stop_process_and_thread(self):
        if self.isRunning():
            self.message_signal.emit("Thread %d is running!" % self.timeStamp)
            if self.process.is_alive():
                self.process.terminate()
                self.process.join()      
            self.stop_thread_signal.emit()   
            #self.terminate() # does it works at this place?       
        else:
            self.message_signal.emit("Thread %d is not running!" % self.timeStamp)

我的应用程序中有两个按钮用于创建/运行和终止线程对象.

I have two buttons in my application to create/run and terminate a thread object.

...
...
# Buttons
self.button_start_thread = QPushButton("Start Thread")
self.button_start_thread.clicked.connect(self.start_thread)
self.button_stop_thread = QPushButton("Stop Thread")
...
...
@Slot()
def start_thread(self):
    self.new_thread = thread_worker(self)
    self.button_stop_thread.clicked.connect(self.new_thread.stop_process_and_thread)
    self.new_thread.stop_thread_signal.connect(self.stop_thread)
    self.new_thread.message_signal.connect(self.print_message)
....
....
@Slot()
def stop_thread(self):
    self.new_thread.terminate()
    #self.button_stop_thread.disconnect(self.new_thread)
    del(self.new_thread)

@Slot(str)
def print_message(self, message):
    print(message)
...
...

如果我开始和停止第一个线程 - 它工作正常并终止,但如果我再次点击停止"按钮,输出是:

If i start and stop the first thread - it works fine and terminate, but if i klick on the 'Stop'-Button again the output is:

Thread 1422117088 is not running!

我不明白:对象self.new_thread 是否被del(self.new_thread) 删除?如果删除了这个对象,我如何访问它?如果我再次启动和停止一个新线程,输出是:

I don't understand it: the object self.new_thread is deleted by del(self.new_thread) or not? How can i access this object if it was deleted? If i start and stop again a new thread, the output is:

Thread 1422117088 is not running!  # not expected, the thread object is deleted!
Thread 1422117211 is running!      # expected

现在我再做一次(开始和停止),输出是:

Now i do it again (start and stop), the output is:

Thread 1422117088 is not running!   # not expected, the thread object is deleted!
Thread 1422117211 is not running!   # not expected, the thread object is deleted!
Thread 1422117471 is running!       # expected

等等...

第一个问题:我不明白为什么不删除旧线程?为什么我可以访问它们?我认为这不好:如果后台线程过多(未删除的对象),我的应用程序会在某个时候崩溃.

First question: I don't understand why the old threads are not deleted? Why i can access them? I think it is not good: my application crashes at some point, if there are too many threads (not deleted objects) in the background.

第二个问题:我不明白为什么删除对象 self.new_thread 后信号不会断开?我不想手动断开信号:如果我有很多信号,我可能会忘记断开一些信号.

Second question: I dont't understand why the signals are not disconnected if i delete the object self.new_thread? I don't want to disconnect the signals manually: if i have many signals i can forgot to disconnect some signals.

第三个问题:我选择这种方式来停止一个进程的线程.还有其他更好的方法吗?

Third question: I choose this way to stop a thread with one process. Is there another way to do this better?

更新:

线程对象似乎已被销毁:

The thread object appears to be destroyed:

del(self.new_thread)
print(self.new_thread)
Output: AttributeError: 'MainWindow' object has no attribute 'new_thread'

但是我的信号没有断开!?这里被描述为:当所涉及的任何一个对象被破坏时,信号槽连接将被删除."它在我的代码中不起作用.

But my signals are not disconnected!? Here is described that: "A signal-slot connection is removed when either of the objects involved are destroyed." It does not work in my code.

推荐答案

你说得对,你的问题是,你的对象没有被删除.您只删除引用 self.new_thread.问题出在这一行:

You're right, your problem is, that your object is not deleted. You delete only the reference self.new_thread. The problem is in this line:

self.new_thread = thread_worker(self)

这样做的原因是线程的父self 还活着.只要 parent 还活着,对象 self.new_thread 就不会被销毁.尝试这样的事情:

The reason for this is that the parent self of the thread is alive. As long as parent stays alive, the object self.new_thread is not destroyed. Try something like this:

self.threadParent = QObject()
self.new_thread = thread_worker(self.threadParent)

现在你也删除了父self.threadParent:

self.new_thread.terminate()
del(self.new_thread)
del(self.threadParent)

现在应该断开您的信号.

Your signals should be disconnected now.

你不需要下面这行,因为在你发出信号stop_thread_signal之后对象self.new_thread已经被删除了:

You don't need the following line, because the object self.new_thread has been already deleted after you emit the signal stop_thread_signal:

#self.terminate() # does it works at this place?

这篇关于Python/PySide:如何销毁终止的线程对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆