user8852560
user8852560

Reputation:

Using threading.RLock Correctly

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?

Upvotes: 1

Views: 434

Answers (1)

soundstripe
soundstripe

Reputation: 1474

I don't know what else you have going on in the app, but I'd recommend taking a look at the wx.CallAfter function. It is thread-safe and can be used to send messages or post events.

import wx
from wx.lib.pubsub import Publisher
import json
from threading import Thread


def update_employee_list(read_file):
    with open(read_file) as f_obj:
        employee_list = json.load(f_obj) # this line should release the GIL so it continues other threads
    # next line sends a thread-safe message to the main event thread
    wx.CallAfter(Publisher().sendMesage, 'updateEmployeeList', employee_list)


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.empList = []
        # subscribe our function to be called when 'updateEmployeeList' messages are received
        Publisher().subscribe(self.updateDisplay, 'updateEmployeeList')

    def updateDisplay(self, employee_list):
        # this assignment should be atomic and thread-safe
        self.empList = employee_list
        # wxPython GUI runs in a single thread, so this is a blocking call
        # if you have many many list items, you may want to modify this method
        # to add one employee at a time to the list to keep it non-blocking.
        self.emp_selection.Append(employee_list)

    def start_read_thread(self):
        filename = 'employee.json'
        t = Thread(target= update_employee_list, args=(filename, ))
        t.start()  # this starts the thread and immediately continues this thread's execution

Update:

Using a with ThreadPoolExecutor blocks because the code is equivalent to:

executor = ThreadPoolExecutor(max_workers=1)
executor.submit(worker_func, args)
executor.shutdown(wait=True)  # <--- wait=True causes Executor to block until all threads complete

You could still use the ThreadPoolExecutor as follows, without the with block. Because you're only :

executor = ThreadPoolExecutor(max_workers=1)
executor.submit(worker_func, args)
executor.shutdown(wait=False)  # <--- threads will still complete, but execution of this thread continues immediately

For more about concurrent futures and Executors, see here for documention.

Upvotes: 1

Related Questions