何时在 GUI 应用程序中调用 thread.join

When to call thread.join in a GUI application

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 组件可以加载。

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

是不好的做法吗

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

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