- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
下面的代码显示了我用于 PyQt 线程功能的类。在我的主程序中,我实例化并启动这个线程(即我使用 PyQt 的 moveToThread,并且 countdown
是 run 方法。)。当线程运行时,我经常调用 reset
方法。我不确定我实现的互斥体是否真的有必要,我希望有人能为我解决这个问题。我执行了一个快速测试,其中注释掉了互斥锁,这没有给我带来任何问题,即使我注释掉 time.sleep(1)
并调用 reset
方法在线程运行时没有任何延迟。但是我不想 100% 安全,因此我在这里提出问题。
import time
from PyQt4.QtCore import *
from threading import Lock
class Countdown(QObject):
finished = pyqtSignal()
def __init__(self, countdownInSec=30):
super(Countdown, self).__init__()
self.COUNTDOWN_IN_SEC = countdownInSec
self._countdownInSec = self.COUNTDOWN_IN_SEC
self._mutex = Lock()
@pyqtSlot()
def countdown(self):
while self._countdownInSec > 0:
print(self._countdownInSec)
self._mutex.acquire()
try:
self._countdownInSec -= 1
finally:
self._mutex.release()
time.sleep(1)
self.finished.emit()
def increment(self, seconds):
self._mutex.acquire()
try:
self._countdownInSec += seconds
finally:
self._mutex.release()
def reset(self):
self._mutex.acquire()
try:
self._countdownInSec = self.COUNTDOWN_IN_SEC
finally:
self._mutex.release()
提取主要内容(仅与该问题相关的部分)
fpIntervUpdCountdownReset = pyqtSignal()
def __init__(self):
self.initFlightPathIntervUpdater()
def initFlightPathIntervUpdater(self):
self.fpIntervUpdCountdownThr = QThread()
self.fpIntervUpdCountdown = countdown.Countdown()
self.fpIntervUpdCountdown.moveToThread(self.fpIntervUpdCountdownThr)
self.fpIntervUpdCountdown.finished.connect(self.fpIntervUpdCountdownThr.quit)
self.fpIntervUpdCountdown.finished.connect(self.flightPathIntervUpdate)
self.fpIntervUpdCountdownThr.started.connect(self.fpIntervUpdCountdown.countdown)
def flightPathIntervUpdateReq(self):
if self.fpIntervUpdCountdownThr.isRunning():
self.fpIntervUpdCountdown.reset()
else:
print 'start'
self.fpIntervUpdCountdownThr.start()
@pyqtSlot()
def flightPathIntervUpdate(self):
print "perform flightPathIntervUpdate"
最佳答案
我会把它留在原处。
如果您不幸的话,您的重置/增量是由一个线程在倒计时线程读取计数器之后和写回其减量结果之前的小时间窗口中执行的。然后它会覆盖更新的值,甚至看不到它。
这个时间窗口非常小,因此它可能不太可能发生,但有可能。
顺便说一下,使用锁的首选方式是:
def increment(self, seconds):
with self._mutex:
self._countdownInSec += seconds
关于python - 这段代码中真的需要互斥锁吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30125318/
这个问题在这里已经有了答案: 关闭 12 年前。 Possible Duplicate: Why can't Python handle true/false values as I expect?
我是不是遗漏了什么或者这是 ruby 中的错误? a = %w(foo bar baz) a.include? "foo" # => true a.size == 3
从 Modelica 文档来看,注释 Evaluate 似乎只对参数有影响: https://build.openmodelica.org/Documentation/ModelicaReferenc
为了避免嵌套的 if 语句并提高可读性,我想创建一个switch(true){ ... } Coldfusion 中的声明。我在 php 中经常使用这个,但是当我在 Coldfusion 中尝试这个时
嗨,我正在尝试处理 ajax json 响应 这是我的代码 success: function (j) { switch(true) { case (j.cho
我之前在我的 TF 代码中使用过这个: count = "${var.whatever == "true" ? 1 : 0}" 这非常适合我想要使用的东西。但是,我正在考虑如何最好地使用类似于说的
我之前在我的 TF 代码中使用过这个: count = "${var.whatever == "true" ? 1 : 0}" 这非常适合我想要使用的东西。但是,我正在考虑如何最好地使用类似于说的
这个问题在这里已经有了答案: How can I return pivot table output in MySQL? (10 个答案) 关闭 5 年前。 我正在尝试构建一个以唯一列值作为列名的表
我制作了一个简单的 JDialog,其中包含一个标签和一个按钮,它基本上相当于信息对话框。所以在对话框中,有一个方法 display() 我在其中调用了 setVisible(true) 五次。 据我
在 bash 4.2.8(1)-release (x86_64-pc-linux-gnu) 在 Ubuntu 11.04 上这个命令 [ $(wc -l /var/www/some.log|cut -
我正在使用 c 语言进行并发处理,我有一个进程池。为此,我让每个 child 都在一个 While (True) 循环中。为了杀死 child ,我正在使用一个全局变量和一个信号处理程序来修改它来打破
我正在尝试选择填写了字段的数据库条目。数据库有两种插入数据的方式,一种输入评论,一种不输入,我希望只选择填写了评论的行。 $requete = "SELECT * FROM daysoff WHER
如何在 JavaMail session 中setDebug(true) 捕获流并在我的日志记录框架中使用它? (缺少下载源代码,更改接受流作为参数的方法,重新编译它,...) 更一般地说,Java
我是 JavaScript 的新手,我刚刚发现了我无法理解的奇怪行为: var magicVar = Math.sin; magicVar == true; // it returns false m
对此感到困惑。 在两台服务器上运行相同版本的 MySQL。 (从完全相同的 rpm 构建)- 沿线的某个地方,一些开发人员改变了一些东西...... 服务器 1: mysql> select ( no
我在查看 OpenSSL 中使用的一些预处理器宏时,从 crypto/stack/safestack.h 中发现了以下内容: #define CHECKED_STACK_OF(type, p) \
所以我遇到了一个问题,我的正则表达式看起来像这样:/true|false/。 当我检查单词 falsee 时,我从这个正则表达式中得到一个 true,有没有办法将它限制为确切的 true 或 fals
我正在对这个恶意 JavaScript 行进行一些试验:var undefined = true; JavaScript 中每个未初始化的变量都有 undefined 的值,这只是一个保存特殊值 'u
我想将 PHP 的微时间存储为我在 MySQL 中的时间戳。 我去过told最好用 DECIMAL 存储它,但我找不到理想的大小。 有谁知道 microtime(true) 返回的最大大小是多少,所以
在 PHP 中,当您在 URL 中包含诸如“var=true”之类的内容时,URL 中的“true”和“false”是否被转换为 boolean 变量,或者它们是否等于文本“true”还是“假”?例如
我是一名优秀的程序员,十分优秀!