【发布时间】:2015-12-18 06:51:04
【问题描述】:
我正在执行一系列线程任务来编写一个文本报告文件,该报告由不同的部分组成。我有两个针对报告的每个部分执行的常用函数(写入标题、写入内容),并且我正在使用 Qthread 池/QRunnable 将写入文件进程与主 GUI 应用程序分开运行。只要在 run 方法中定义了任务,该方法似乎就可以正常工作,但是当我尝试从 QRunnable 的每个类的 run 方法调用全局函数时,我的程序崩溃(我得到一个 SQLITE db 错误)(对应到每个报告部分)。
示例:
class Application:
def report_to_file(self,file_path):
self.report.file_path = val_file_path
self.long_task = QThreadPool()
self.long_task.setMaxThreadCount(1)
start_report = StartReport(val_file_path)
check_dups = DupStreetDesc()
third_section = ThirdSection()
start_report.signals.result.connect(self.process_results)
check_dups.signals.result.connect(self.process_results)
third_section.result.connect(self.process_results)
self.long_task.start(start_report)
self.long_task.start(check_dups)
self.long_task.start(third_section)
self.long_task.waitForDone()
def process_results(self):
print "the task has been executed successfully!"
def finished(self):
QMessageBox.information(self.validation_dia,"Message","Task finished")
在另一个模块中,我有我的 Runnables 并将函数编写为全局变量(不包含在类中)
global report_file
global queries
def write_content(query_id, header_id, header_no_content_id, columns_name_id, include_footpaths=True, include_subtitle=False):
# in here I query the db and write text into the file, this function is used from all the Qrunnable classes
def write_headers(section_header, include_footpaths=False, include_subtitle=False):
# here I write other content to the same file ,this function is used from all the Qrunnable classes
class StartReport(QRunnable):
def __init__(self, file_path):
super(StartReport,self).__init__()
self.signals = GeneralSignals()
self.file_path = file_path
self.task = "Starting the Report"
# print "on report start report file is " + str(self.report_file)
self.org_name = org_name
self.user = user
self.report_title = "Roadnet Validation Report"
global progress_win
self.progress_win = progress_win
self.progress_win.setLabelText("Report Initialisation")
self.progress_win.show()
def run(self):
if self.file_path is None:
return
else:
global report_file
# create the file and prints the header
report_file = open(self.file_path, 'wb')
report_file.write(str(self.report_title) + ' for {0} \n'.format(self.org_name))
report_file.write('Created on : {0} at {1} By : {2} \n'.format(datetime.today().strftime("%d/%m/%Y"),
datetime.now().strftime("%H:%M"),
str(self.user)))
report_file.write(
"------------------------------------------------------------------------------------------ \n \n \n \n")
self.signals.result.emit(self.task)
在上述情况下,函数运行是因为一切都发生在 run 方法中
下例进入全局函数时进程崩溃
class DupStreetDesc(QRunnable):
"""
duplicate street description report section creation
:return: void if the report is to text
list[string] if the report is to screen
"""
def __init__(self):
# print(StartReport.report_file)
self.signals = GeneralSignals()
self.task = "Checking duplicate street descriptions..."
super(DupStreetDesc,self).__init__()
# report_file
# self.report_file = report_file
global progress_win
self.progress_win = progress_win
self.progress_win.setLabelText("Checking duplicate street descriptions...")
def run(self):
global report_file
global queries
print "I run " + " report file is " + str(report_file)
print " these are queries " + str(queries)
print " this is the connection " + str(db)
if report_file is None:
items_list = write_content(0, 0, 0, 0)
self.progress_win.setValue(1)
return items_list
else:
# here is where I call the global functions
write_headers(0)
write_content(0, 0, 0, 0)
progress_win.setValue(1)
self.signals.result.emit(self.task)
class GeneralSignals(QObject):
result = pyqtSignal(str)
我是 Pyqt 中这个 Qthread 池类的新手,到目前为止,在我看到的所有示例中,函数都在 run 方法中运行,而不调用任何外部函数。我不知道这是否可能,谁能告诉我实现这一目标的正确方法?
【问题讨论】:
标签: multithreading python-2.7 pyqt threadpool