I'm creating a utility in Python that reads data in from a file on startup on a separate thread so that the rest of the GUI components can load. The data gets stored into a list
and then appended to a combobox. How would I lock the list
so that no other method can call the list at the same time it's being used by the def read_employees(self, read_file):
method.
This is the best attempt I can come up with.
#left out imports
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.lock = threading.RLock()
self.empList = []
def read_employees(self, read_file):
with open(read_file) as f_obj:
employees = json.load(f_obj)
with self.lock:
self.empList = [empEmail for empEmail in employees.keys()]
wx.CallAfter(self.emp_selection.Append, self.empList)
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()
Is using RLock
appropriate here?