Python多线程工作负载崩溃

时间:2015-12-05 20:37:36

标签: python multithreading python-multithreading

#!/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()

为什么这个垃圾SegFaulting?

我需要在命令()运行的同时刷新UI,并在执行此操作时更新UI。

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

但它会在运行时立即抛出Segmentation Fault,代码为139。

1 个答案:

答案 0 :(得分:0)

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

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