编辑:太多的信息会分散人们的注意力,所以我几乎删除了所有内容。这是在 Windows 上,Python 3.6 使用 mmap 和 msvcrt
一个 python 应用程序中的代码:
fd = os.open(r'C:\somefile', os.O_CREAT | os.O_RDWR)
mm_file = mmap.mmap(fd, 4096, access=mmap.ACCESS_WRITE)
msvcrt.locking(fd, msvcrt.LK_LOCK, 4096)
Run Code Online (Sandbox Code Playgroud)
当第二个应用程序尝试打开 C:\somefile 进行读/写时,预期会收到某种错误消息,表明它无法访问它,因为它已被锁定。
实际发生的情况:第二个应用程序访问它没有问题。
我有一个列表控件,
self.list = wx.ListCtrl(panel, -1, style=wx.LC_REPORT | wx.LC_NO_HEADER)
self.list.InsertColumn(col=0, heading='', format=wx.LIST_FORMAT_CENTER, width=150)
self.list.InsertColumn(col=1, heading='', format=wx.LIST_FORMAT_CENTER, width=450)
for person in people:
#this is the issue right here
index = self.list.InsertItem(0, person.age) #one line to insert an item and get the index, and the next line to update the item at that index... two lines to actually put a full entry in.
self.list.SetItem(index=index, column=1, label=person.name)
Run Code Online (Sandbox Code Playgroud)
最初在构造函数中设置 listctrl 效果很好,但是如果我想在运行时动态添加/删除 listctrl 中的项目怎么办?
我遇到过 wx.CallAfter、wx.CallLater、startWorker (来自 wx.lib.delayedresult)和 wx.Timer
如果您查看上面的示例,问题是我有一行插入该项目,另一行更新该项目以在刚刚插入的项目上具有正确的名称。因此,如果我有线程轮流删除和添加项目到 listctrl,如果我插入一个项目,而另一个线程同时插入一个项目,那么我刚刚获得的索引将与更新无关。即我需要一个原子操作来插入一个项目,其中包括插入人的年龄和人的名字。所以我的第一个问题是,有没有办法将列表项的所有信息插入一行?
如果我不能做到这一点,那么我的下一个问题是我怎样才能完成规定的行为?例如,假设有线程随机对顶行进行着色、添加和删除:
self.color_timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, …Run Code Online (Sandbox Code Playgroud)