RT,官方給的檔案是c++的,寫了后始終有保存內容“Process finished with exit code 1073741845”。我的多執行緒代碼如下:
from PySide2.QtCore import QThread
from PySide2.QtCore import QObject
from PySide2.QtWidgets import QApplication
from PySide2.QtWidgets import QFileDialog
# 信號槽
from PySide2.QtCore import Signal
from PySide2.QtCore import Slot
from process_package.process_data_module import TrackDataProcessing
class FileManager(QObject):
def __init__(self):
super(FileManager, self).__init__()
self.__init_instance()
self.__init_connections()
def __del__(self):
# print('delete')
self.__split_data_thread.quit()
self.__split_data_thread.wait()
def split_data_to_new_file(self):
"""
打開對話框選擇要拆分資料的檔案,將檔案名串列傳給后端處理
"""
# 獲取打開的所有檔案名串列
files_in_list = QFileDialog.getOpenFileNames()[0]
# 打開檔案數不為0,將檔案名串列傳給后端處理
if 0 != len(files_in_list):
# 判斷子執行緒狀態,如果沒有啟動,則啟動子執行緒
self.__start_sub_thread(self.__split_data_thread, self.__track_processing)
self.si_file_name_with_path.emit(files_in_list)
@Slot()
def __close_application(self):
"""
退出程式
"""
QApplication.closeAllWindows()
def __init_instance(self):
"""
初始化物件變數
"""
self.__track_processing = TrackDataProcessing()
self.__split_data_thread = QThread()
def __init_connections(self):
"""
初始化信號槽
"""
# 信號槽新寫法,推薦
# 向槽函式傳遞打開檔案名的串列
self.si_close_application.connect(self.__close_application)
def __start_sub_thread(self, thread: QThread, process: QObject):
"""
啟動將變數遷入子執行緒,啟動子執行緒
:param thread: 子執行緒
:param process: 遷入子執行緒的變數
"""
self.si_file_name_with_path.connect(self.__track_processing.split_data_to_different_file)
if not thread.isRunning():
process.moveToThread(thread)
thread.finished.connect(process.deleteLater)
self.si_file_name_with_path.connect(self.__track_processing.split_data_to_different_file)
thread.start()
# 打開的檔案名及檔案路徑
si_file_name_with_path = Signal(list)
si_close_application = Signal()
我在“__del__(self)”中終止行程,在“__start_sub_thread”啟動子執行緒,同時創建信號槽。后續讀取資料什么操作都可以正常進行,就是關閉程式后有前面哪行例外,不知道執行緒出錯到哪里了。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/264621.html
下一篇:scrapy框架 古詩文網爬取
