【问题标题】:When to call thread.join in a GUI application何时在 GUI 应用程序中调用 thread.join
【发布时间】:2017-11-14 16:27:10
【问题描述】:
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()打电话是不好的做法吗?

【问题讨论】:

    标签: python python-3.x wxpython python-multithreading


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多