Python 3.x 在Gui应用程序python中启动新线程

Python 3.x 在Gui应用程序python中启动新线程,python-3.x,gtk,python-multithreading,Python 3.x,Gtk,Python Multithreading,我有一个PythonGUI应用程序,它有一个线程可以进行一些更新。 这就是它的实施方式 GObject.threads_init() Class main: #Extra stuff here update_thread = Thread(target= update_func, args=(Blah blah,)) update_thread.setDaemon(True) update_thread.start() Gtk.main() 这就是更新函

我有一个PythonGUI应用程序,它有一个线程可以进行一些更新。 这就是它的实施方式

GObject.threads_init()
Class main:
    #Extra stuff here
    update_thread = Thread(target= update_func, args=(Blah blah,))
    update_thread.setDaemon(True)
    update_thread.start()
    Gtk.main()
这就是更新函数的样子

def update_func():
    try:
        #do updating
        time.sleep(#6hrs)
    except:
        #catch error 
        time.sleep(#5 min)
    finally:
        update_func()
只要程序在运行,线程就会运行,我的程序运行了几天 问题是,有时线程死亡,更新没有发生,我必须重新启动应用程序


如果当前线程死亡,有没有办法启动新线程,特别是在Gui应用程序中?

下面是一个从我的gtk应用程序中剪下的线程示例。这可能会有帮助

#!/usr/bin/env python3

# Copyright (C) 2013 LiuLang <gsushzhsosgsu@gmail.com>

# Use of this source code is governed by GPLv3 license that can be found
# in http://www.gnu.org/licenses/gpl-3.0.html


from gi.repository import GObject
from gi.repository import Gtk
import threading

#UPDATE_INTERVAL = 6 * 60 * 60 * 1000   # 6 hours
UPDATE_INTERVAL = 2 * 1000   # 2 secs, for test only

def async_call(func, func_done, *args):
    '''
    Call func in another thread, without blocking gtk main loop.

    `func` does time-consuming job in background, like access website.
    If `func_done` is not None, it will be called after func() ends.
    func_done is called in gtk main thread, and this function is often used
    to update GUI widgets in app.
    `args` are parameters for func()
    '''
    def do_call(*args):
        result = None
        error = None

        try:
            result = func(*args)
        except Exception as e:
            error = e

        if func_done is not None:
            GObject.idle_add(lambda: func_done(result, error))

    thread = threading.Thread(target=do_call, args=args)
    thread.start()

class App(Gtk.Window):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.set_default_size(480, 320)
        self.set_border_width(5)
        self.set_title('Gtk Threads')
        self.connect('delete-event', self.on_app_exit)

        self.update_timer = GObject.timeout_add(UPDATE_INTERVAL,
                self.check_update)

    def run(self):
        self.show_all()
        Gtk.main()

    def on_app_exit(self, *args):
        Gtk.main_quit()

    def check_update(self):
        def _do_check_update():
            print('do check update: will check for updating info')
            print(threading.current_thread(), '\n')

        print('check update')
        print(threading.current_thread())
        async_call(_do_check_update, None)
        return True


if __name__ == '__main__':
    app = App()
    app.run()
#/usr/bin/env蟒蛇3
#版权所有(C)2013流浪
#此源代码的使用受可以找到的GPLv3许可证的约束
#在http://www.gnu.org/licenses/gpl-3.0.html
从gi.repository导入GObject
从gi.repository导入Gtk
导入线程
#更新间隔=6*60*60*1000#6小时
更新间隔=2*1000#2秒,仅用于测试
def异步调用(func,func_done,*args):
'''
在另一个线程中调用func,而不阻塞gtk主循环。
`func`在后台做一些耗时的工作,比如访问网站。
如果'func_done'不是None,则将在func()结束后调用它。
func_done在gtk主线程中被调用,这个函数经常被使用
更新应用程序中的GUI小部件。
`args`是func()的参数
'''
def do_调用(*参数):
结果=无
错误=无
尝试:
结果=func(*args)
例外情况除外,如e:
误差=e
如果func_done不是无:
GObject.idle\u add(lambda:func\u done(结果,错误))
thread=threading.thread(target=do\u call,args=args)
thread.start()
类应用程序(Gtk.窗口):
定义初始化(自):
super()。\uuuu init\uuuuu()
self.initUI()
def initUI(self):
设置默认大小(480320)
自行设置边框宽度(5)
自我设置标题(“Gtk螺纹”)
self.connect('delete-event',self.on\u app\u exit)
self.update\u timer=GObject.timeout\u add(更新间隔,
自我检查(更新)
def运行(自):
self.show_all()
Gtk.main()
应用程序退出时的def(自身,*参数):
Gtk.main_quit()
def检查_更新(自我):
def_do_check_update():
打印('执行检查更新:将检查更新信息')
打印(threading.current_thread(),“\n”)
打印('检查更新')
打印(线程。当前线程()
异步调用(\u执行\u检查\u更新,无)
返回真值
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
app=app()
app.run()