gpt4 book ai didi

python - 正确使用 threading.RLock

转载 作者:太空宇宙 更新时间:2023-11-03 14:31:13 25 4
gpt4 key购买 nike

我正在 Python 中创建一个实用程序,该实用程序在启动时在单独的线程上从文件中读取数据,以便可以加载其余的 GUI 组件。数据存储到列表中,然后附加到组合框。我如何锁定列表,以便在 def read_employees(self, read_file): 方法使用该列表的同时,没有其他方法可以调用该列表。

这是我能想到的最好的尝试。

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

这里使用RLock合适吗?

最佳答案

我不知道您在应用程序中还进行了哪些操作,但我建议您查看一下 wx.CallAfter 函数。它是线程安全的,可用于发送消息或发布事件。

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

更新:

使用带有 ThreadPoolExecutor 的 block ,因为代码等效于:

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

您仍然可以按如下方式使用 ThreadPoolExecutor,而不使用 with block 。因为你只是:

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

有关并发 future 和执行器的更多信息,see here for documention .

关于python - 正确使用 threading.RLock,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47275734/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com