When to call thread.join in a GUI application

夙愿已清 提交于 2019-12-02 07:53:55

问题


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()

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.

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()


回答1:


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

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.



来源:https://stackoverflow.com/questions/47290677/when-to-call-thread-join-in-a-gui-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!