何时在 GUI 应用程序中调用 thread.join [英] When to call thread.join in a GUI application

查看:24
本文介绍了何时在 GUI 应用程序中调用 thread.join的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import wx
import json
import queue
from collections import namedtuple
import threading


class MyDialog(wx.Frame):

    def __init__(self, parent, title):
        self.no_resize = wx.DEFAULT_FRAME_STYLE & ~ (wx.RESIZE_BORDER | wx.MAXIMIZE_BOX)
        wx.Frame.__init__(self, parent, title=title, size=(500, 450),style = self.no_resize)

        self.panel = wx.Panel(self, size=(250, 270))
        self.emp_selection = wx.ComboBox(self.panel, -1, pos=(40, 50), size=(200,100))
        self.start_read_thread()

        #code to load other GUI components             

        self.Centre()
        self.Show(True)


    def read_employees(self, read_file):
        list_of_emails = queue.Queue()
        with open(read_file) as f_obj:
            employees = json.load(f_obj)
        list_of_emails = [empEmail for empEmail in employees.keys()]
        wx.CallAfter(self.emp_selection.Append, list_of_emails)

    def start_read_thread(self):
        filename = 'employee.json'
        empThread = threading.Thread(target=self.read_employees, args=(filename,))
        empThread.start()

我有一个加载组合框的 GUI 应用程序,并启动一个线程来读取一些数据并将其加载到组合框中.我不希望读取被阻塞,以便其他 GUI 组件可以加载.

I have a GUI application that loads a combobox, and starts a thread to read some data and load it into the combobox. I don't want the read to block, so that the other GUI components can load.

在调用thread.start()之后什么时候调用thread.join()合适?根据我的理解,join() 等待线程完成,我不希望那样,我想启动线程并允许所有其他组件加载.不调用 join()

After calling thread.start() when is it appropriate to call thread.join()? From my understanding, join() waits for the thread to complete, I don't want that, I want to start the thread and allow all the other components to load. Is it bad practice not to call join()

推荐答案

如果您不需要 join() 的功能来等待线程完成,则完全可以不调用它.

It is perfectly ok to not call join() if you don't need its functionality to wait for the thread to finish.

顺便说一下:在 GUI 应用程序的主线程(在启动时自动创建的所有 GUI 事情发生的线程)中,调用任何睡眠或等待任何东西的函数是不好的做法,因为 GUI 没有反应(冻结)等待时.

By the way: In the main thread of a GUI application (the thread automatically created at start in which all GUI things happen) it is bad practice to call any function that sleeps or waits for anything as the GUI doesn't react (freezes) while waiting.

这篇关于何时在 GUI 应用程序中调用 thread.join的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆