【发布时间】:2017-07-30 15:43:09
【问题描述】:
我正在使用 python 3.5.3 和 PyQT 5,并且我已经用它编写了 GUI。 这个 GUI 使用 subprocess.run 运行 python 代码 python 代码。
为了在子进程操作期间让我的 GUI 处于活动状态而不是冻结,我在线程中运行子进程。
在 GUI 我有停止按钮,如果用户按下,我想终止子进程。
使用线程的终止方法来终止线程没有问题。 但这不会终止子进程。
我尝试使用 Popen 代替 run,但我无法让它像 subprocess.run 那样运行。 另外,我更喜欢使用 Python 推荐的方式,这也给了我 check_error 选项
这就是我使用子流程的方式:
class c_run_test_thread(QtCore.QThread):
def __init__(self,test_file,log_file):
QtCore.QThread.__init__(self)
self.test_file = test_file
self.log_file = log_file
def __del__(self):
self.wait()
def run(self):
# Thread logic
try:
# Run the test with all prints and exceptions go to global log file
self.test_sub_process = subprocess.run(["python", self.test_file],stdout = self.log_file, stderr = self.log_file,check = True)
except subprocess.CalledProcessError as error:
print("Error : {}".format(error))
# Flush memory to file
self.log_file.flush(
def stop(self):
# Flush memory to file
self.log_file.flush()
我通过
终止线程# Stop test thread
self.thread_run_test.terminate()
总而言之,我想在杀死子进程的同时杀死线程。
【问题讨论】:
标签: python python-3.x pyqt