Python锁定问题

时间:2017-04-06 20:33:13

标签: python multithreading tkinter locking

我正在尝试使用Locks来处理多线程应用程序中共享资源的线程访问,并且事情没有按预期运行。请参阅以下代码块(python)

def update(self, **kwargs):
    updated = False
    print("attempting to acquire display mutex for update")
    if self._updateMutex.acquire(blocking=False):
        print("display mutex acquired for update")
        time.sleep(1)
        print("waking up")
        self.progressBar.config(style='Vertical.TProgressbar')
        print("attempting to release mutex for update")
        self._updateMutex.release()
        print("display mutex released")
    print("done...")

def shutdown(self):
    print("trying shutdown")
    self._updateMutex.acquire()
    print("mutex acquired...")
    self._updateMutex.release()
    print("shutdown done")

这是输出......

attempting to acquire display mutex for update
display mutex acquired for update
trying shutdown
waking up

不,这实际上并不是我所运行的,但它说明了这个想法。我在更新方法中插入了一个延迟,以便我期望应该发生的是:

  1. 更新方法被称为
  2. 更新方法获取互斥锁并等待计时器
  3. 调用shutdown方法并请求互斥锁,但它目前由'update'拥有,它正在休眠,因此关闭阻塞并等待互斥锁释放
  4. 更新唤醒并完成其功能,最终释放互斥锁
  5. 关闭通知可以继续并完成其功能
  6. 我假设输出应该如下所示:

    ...
    attempting to acquire display mutex for update
    display mutex acquired for update
    trying shutdown
    waking up
    attempting to release mutex for update
    display mutex released
    #'done...' might appear on any line now
    mutex acquired...
    shutdown done
    

    显然,这不是正在发生的事情,我不知道为什么。问题似乎集中在tkinter GUI对象上设置样式(或以其他方式与之交互)。在设置样式的调用之前,事情不会停止(您可以看到发送'唤醒'消息)。

    除了在__init __()中调用构造函数之外,这些是代码中锁/互斥锁的唯一两个引用。

    从字面上看,我在'关闭'中所做的唯一事情就是获取并释放互斥锁;如果我不这样做,一切正常:更新继续chugging并正常退出。只有在“关闭”中获得互斥锁时才会失败。有什么想法发生了什么?我的假设怎么样?

0 个答案:

没有答案