【问题标题】:Python Multi-Threaded workload crashingPython 多线程工作负载崩溃
【发布时间】:2015-12-08 22:25:24
【问题描述】:
#!/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?

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

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

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

【问题讨论】:

  • 启动线程后,您的程序不会等待它们运行。尝试在创建线程后添加一个 while True: time.sleep(1.0)。
  • 做到了!多线程新手,不知道!发布我的upvote答案!如果 name == 'main': threading.Thread(target = ui).start() sleep(1) threading.Thread(target = main).start()睡眠(1)

标签: python multithreading python-multithreading


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2021-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-15
    • 1970-01-01
    • 2015-12-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多