我想实现一个按钮来停止带有进程的线程,它可以正常工作,但没有达到预期效果:我无法删除线程对象。 (编辑:似乎删除了对该线程对象的引用,但是通过删除线程对象并不会自动断开信号的连接,我仍然可以通过信号来访问它。)

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

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)

我的应用程序中有两个按钮可以创建/运行和终止线程对象。
...
...
# 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)
...
...

如果我启动和停止第一个线程-它可以正常工作并终止,但是如果我再次按下“停止”按钮,则输出为:
Thread 1422117088 is not running!

我不明白:对象self.new_thread是否已被del(self.new_thread)删除?如果删除了该对象,我该如何访问?如果我启动并再次停止新线程,输出为:
Thread 1422117088 is not running!  # not expected, the thread object is deleted!
Thread 1422117211 is running!      # expected

现在我再做一次(开始和停止),输出为:
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

等等...

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

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

第三个问题:
我选择这种方式来停止一个进程的线程。还有另一种方法可以更好地做到这一点吗?

更新:

线程对象似乎已被破坏:
del(self.new_thread)
print(self.new_thread)
Output: AttributeError: 'MainWindow' object has no attribute 'new_thread'

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

最佳答案

没错,您的问题是您的对象没有被删除。您仅删除引用self.new_thread。问题在这一行:

self.new_thread = thread_worker(self)

这样做的原因是线程的父self仍然有效。只要父代保持生命,对象self.new_thread就不会被破坏。尝试这样的事情:
self.threadParent = QObject()
self.new_thread = thread_worker(self.threadParent)

现在,您还删除了父self.threadParent:
self.new_thread.terminate()
del(self.new_thread)
del(self.threadParent)

您的信号现在应断开连接。

您不需要以下行,因为发出信号self.new_thread后,对象stop_thread_signal已被删除:
#self.terminate() # does it works at this place?

10-06 10:21
查看更多