队列和列表访问元素

时间:2017-11-19 16:31:22

标签: python multithreading wxpython

我有一个GUI应用程序,它使用一个单独的线程来读取一些数据,创建一个列表并将其加载到一个组合框中。

示例:

class MyDialog(wx.Frame):

    def __init__(self, parent, title):

        lock = threading.Lock()
        self.list_of_emails = []
        self.start_read_thread()

    def read_employees(self, read_file):
        list_of_emails = []

        with lock:
            with open(read_file) as f_obj:
               #code to read and split the data and create an employee object left out
               list_of_emails = [employee.email for employee in employees]


        wx.CallAfter(self.emp_selection.Append, list_of_emails)

    def start_read_thread(self):
        filename = 'employee.json'
        with concurrent.futures.ThreadPoolExecutor(max_workers=1) as executor:
            executor.submit(self.read_employees, filename)


app = wx.App(False)
frame = MyDialog(None, "Crystal Rose")
app.MainLoop()

我的问题是我还必须从主线程访问同一个列表self.list_of_emails = []。当用户从组合框中选择一个员工时,我将根据索引提取相应的员工对象。当我准备好访问self.list_of_emails = []时,应该完成使用它的线程。

问题:

  1. 但是,假设self.start_read_thread()没有完成,在主线程中我是否需要检查线程是否仍处于活动状态,如果是,请在使用列表前使用锁定对象?

  2. 我知道Queue,问题是,我不知道如何通过索引访问特定元素,我知道的唯一工作就是创建一个临时的list。但是,无论如何,这对我来说似乎效率低下。在没有明确创建Queue并按索引访问元素的情况下,是否还要像list一样处理list

0 个答案:

没有答案