Igo*_*gor 2 python multithreading pyqt multiprocessing pyside
我想实现一个按钮来停止一个进程的线程,它的工作原理但不是预期的:我无法删除线程对象.(编辑:对线程对象的引用似乎被删除,但是通过删除线程对象不会自动断开信号,我无论如何都可以通过信号访问它.)
我有一个带有类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)
Run Code Online (Sandbox Code Playgroud)
我的应用程序中有两个按钮来创建/运行和终止线程对象.
...
...
# 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)
...
...
Run Code Online (Sandbox Code Playgroud)
如果我开始和停止第一个线程 - 它工作正常并终止,但如果我再次点击'停止'按钮输出是:
Thread 1422117088 is not running!
Run Code Online (Sandbox Code Playgroud)
我不明白:对象self.new_thread被删除del(self.new_thread)与否?如果删除了该对象,该如何访问?如果我再次启动和停止新线程,则输出为:
Thread 1422117088 is not running! # not expected, the thread object is deleted!
Thread 1422117211 is running! # expected
Run Code Online (Sandbox Code Playgroud)
现在我再做一次(开始和停止),输出是:
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
Run Code Online (Sandbox Code Playgroud)
等等...
第一个问题: 我不明白为什么旧线程没有被删除?为什么我可以访问它们?我认为这不好:如果后台有太多线程(未删除的对象),我的应用程序会在某些时候崩溃.
第二个问题:
如果删除对象,我不明白为什么信号没有断开self.new_thread?我不想手动断开信号:如果我有很多信号,我可能忘记断开某些信号.
第三个问题: 我选择这种方式来停止一个进程的线程.还有另一种方法可以做得更好吗?
更新:
线程对象似乎被破坏:
del(self.new_thread)
print(self.new_thread)
Output: AttributeError: 'MainWindow' object has no attribute 'new_thread'
Run Code Online (Sandbox Code Playgroud)
但我的信号没有断开!?这里描述:"当涉及的任何一个对象被破坏时,信号槽连接被移除." 它在我的代码中不起作用.
你的问题是,你的对象没有被删除.您只删除引用self.new_thread.问题出在这一行:
self.new_thread = thread_worker(self)
Run Code Online (Sandbox Code Playgroud)
原因是self线程的父级是活动的.只要父母保持活着,对象self.new_thread就不会被破坏.尝试这样的事情:
self.threadParent = QObject()
self.new_thread = thread_worker(self.threadParent)
Run Code Online (Sandbox Code Playgroud)
现在你也删除了父母self.threadParent:
self.new_thread.terminate()
del(self.new_thread)
del(self.threadParent)
Run Code Online (Sandbox Code Playgroud)
现在应该断开你的信号.
您不需要以下行,因为在self.new_thread发出信号后该对象已被删除stop_thread_signal:
#self.terminate() # does it works at this place?
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11721 次 |
| 最近记录: |