python threading lock和rlock_正确使用threading.RLock

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?

解决方案

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

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值