简体   繁体   中英

Python Multi-Threaded workload crashing

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

Why is this junk SegFaulting?

I need for the UI to refresh at the same time as commands() thing runs, and update the UI while it does that.

To test it, i set up a small for-loop.

But it throws and Segmentation Fault immediately as soon at is run, with code 139.

Try adding a while True loop after the thread creation:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM