Python 多线程工作负载崩溃


#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#  untitled3.py
#  
#  Copyright 2015 Ognjen Galic <gala@thinkpad>
#  
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#  
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#  
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#  MA 02110-1301, USA.
#  
#  
import commands
import threading
from ui import Ui_main_window
from PyQt4 import QtGui, QtCore
from time import sleep
import sys
class MainWindow(QtGui.QMainWindow, Ui_main_window):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.setupUi(self)
def do_sudo(command):
    # do stuffs with commands()
def ui():
    app = QtGui.QApplication(sys.argv)
    ui.MainWindow()
    ui.show()
def main():
    ui = MainWindow()
    for i in range(1,100):
            ui.progressBar.setValue(i)
            sleep(1)
if __name__ == '__main__':
    threading.Thread(target = ui).start()
    threading.Thread(target = main).start()

为什么这个垃圾赛格错误?

我需要 UI 在 command(( 运行的同时刷新,并在运行时更新 UI。

为了测试它,我设置了一个小的for循环。

但它在运行后立即抛出和分段错误,代码为 139。

尝试在线程创建后添加 while True 循环:

if __name__ == '__main__':
    threading.Thread(target = ui).start()
    threading.Thread(target = main).start()
    while True:
        sleep(1.0)

最新更新