Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 正确使用threading.RLock_Python_Multithreading - Fatal编程技术网

Python 正确使用threading.RLock

Python 正确使用threading.RLock,python,multithreading,Python,Multithreading,我正在用Python创建一个实用程序,它在单独的线程上启动时从文件中读取数据,以便可以加载其余的GUI组件。数据存储到列表中,然后附加到组合框中。如何锁定列表,以便其他方法无法在def read_员工(self,read_文件)使用列表的同时调用列表:方法 这是我能想到的最好的尝试 #left out imports class MyDialog(wx.Frame): def __init__(self, parent, title): self.no_resize

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

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

#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一起使用会阻塞,因为代码等效于:

executor = ThreadPoolExecutor(max_workers=1)
executor.submit(worker_func, args)
executor.shutdown(wait=True)  # <--- wait=True causes Executor to block until all threads complete
executor=ThreadPoolExecutor(最大工作线程数=1)
执行人提交(工人函数,参数)

executor.shutdown(wait=True)#感谢您花时间和精力创建此文件。在
start\u read\u线程中,
self.read\u employees
应该是
update\u employeelist(read\u文件):
def update\u employeelist(read\u文件):
updateEmployeeList'
应该是
updateDisplay
正确吗?如果我想继续使用
R.Lock
,我是否正确使用了它?是的,我已经修复了该错误。在您的示例中,ThreadExecutor将一直阻塞,直到线程完成,所以我不能说它是正确的。至于锁,很难判断你是否正确使用了它,因为我只看到它的一种用法。如果要使用锁,则必须在访问该变量的任何地方使用它。那个看起来不错,虽然有。我需要CallAfter处于锁定区域。您能否更新您的答案以解释
threadpoolexecutor
将被阻止的原因?在这里的回答中,有人建议我使用
threadpoolexecutor
。文档中说,如果我将
一起使用,我就不必使用shutdown。它将起作用,就像
shutdown
被设置为True一样。不完全如此。请仔细阅读我的更新。如果
wait=True
,代码将阻塞,直到所有线程完成
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