Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.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中的线程(我有一个无止境的循环)_Python_Multithreading_Kill - Fatal编程技术网

如何停止Python中的线程(我有一个无止境的循环)

如何停止Python中的线程(我有一个无止境的循环),python,multithreading,kill,Python,Multithreading,Kill,以下代码适用于倒计时计时器: 从org.csstudio.opibuilder.scriptUtil导入PVUtil、ConsoleUtil、ColorFontUtil 从java.lang导入线程,可运行 导入线程 导入时间 startButton=display.getWidget(“启动按钮”) stopButton=display.getWidget(“停止按钮”) resetButton=display.getWidget(“Reset_按钮”) bar=display.getWidg

以下代码适用于倒计时计时器:

从org.csstudio.opibuilder.scriptUtil导入PVUtil、ConsoleUtil、ColorFontUtil
从java.lang导入线程,可运行
导入线程
导入时间
startButton=display.getWidget(“启动按钮”)
stopButton=display.getWidget(“停止按钮”)
resetButton=display.getWidget(“Reset_按钮”)
bar=display.getWidget(“进度条”)
ECurrents=display.getWidget(“电子当前”)
PCurrents=display.getWidget(“正电子当前”)
ELifetimes=display.getWidget(“电子寿命”)
PLifetimes=display.getWidget(“正电子寿命”)
minText=display.getWidget(“minText”)
minPV=minText.getPV()
secText=display.getWidget(“secText”)
secPV=secText.getPV()
timerLabel=display.getWidget(“timerLabel”)
类闪烁(可运行):
def运行(自):
i=0
而PVUtil.getLong(pvs[2])==1:
线程。睡眠(500)
timerLabel.setPropertyValue(“前景色”,
如果i%2==0,则为ColorFontUtil.YELLOW,否则为ColorFontUtil.RED)
i=i+1
timerLabel.setPropertyValue(“前景颜色”,ColorFontUtil.BLACK)
类计时器(可运行):
def运行(自):
startButton.setEnabled(错误)
stopButton.setEnabled(真)
resetButton.setEnabled(错误)
bar.setVisible(真)
minText.setEnabled(False)
secText.setEnabled(错误)
min=PVUtil.getLong(minPV)
sec=PVUtil.getLong(secPV)
#记住要重置的值
resetButton.setVar(“最小”,120)
resetButton.setVar(“秒”,0)
timerLabel.setPropertyValue(“前景颜色”,ColorFontUtil.BLACK)
timerLabel.setPropertyValue(“文本”,“剩余注入时间:”)
ECurrents.setPropertyValue(“背景颜色”,ColorFontUtil.GREEN)
setPropertyValue(“背景颜色”,ColorFontUtil.GREEN)
PLifetimes.setPropertyValue(“背景颜色”,ColorFontUtil.GREEN)
PCurrents.setPropertyValue(“背景颜色”,ColorFontUtil.GREEN)
停止=错误
总计=(最小值*60)+(秒)
对于范围内的i(总计-1,-1):
如果不是,则显示.isActive():
返回
如果PVUtil.getLong(pvs[0])==0:
停止=真
打破
pvs[1]。设定值(100-100*i/总计)
最小设定值(整数(i/60))
secPV.setValue(int(i%60))
线程。睡眠(1000)
timerLabel.setPropertyValue(“前景颜色”,ColorFontUtil.RED)
如果(总数=300):
timerLabel.setPropertyValue(“文本”,“注射前5分钟!”)
PCurrents.setPropertyValue(“背景颜色”,ColorFontUtil.RED)
ECurrents.setPropertyValue(“背景颜色”,ColorFontUtil.RED)
ELifetimes.setPropertyValue(“背景颜色”,ColorFontUtil.RED)
PLifetimes.setPropertyValue(“背景颜色”,ColorFontUtil.RED)
如果停止:
timerLabel.setPropertyValue(“文本”,“中断!”)
其他:
timerLabel.setPropertyValue(“文本”,“时间到了!!!”)
pvs[2]。设置值(1)
线程(闪烁()).start()
widget.executeAction(0)
startButton.setEnabled(真)
stopButton.setEnabled(错误)
resetButton.setEnabled(真)
bar.setVisible(假)
minText.setEnabled(真)
secText.setEnabled(真)
如果PVUtil.getLong(pvs[0])==1:
线程=线程(计时器());
thread.start()
“widget.executeAction(0)”行应该是计时器完成倒计时时播放的声音,但我遇到的问题是声音一直在无休止地播放。如何终止线程,使其只执行一次?

“我遇到的问题是声音一直在无休止地播放”

这无疑是一个重复的线程调用问题。(如果您提供调用您在上面发布的代码的代码,我相信我们可以帮助您找到它。)

另外,在代码中设置断点或消息框,让它告诉您(在线程启动之前和线程运行期间),这样您就可以知道它被多次调用了。。。什么时候

希望这有帮助

编辑:

if PVUtil.getLong(pvs[0])==1:

thread =Thread(Timer());

thread.start()

您正在线程中启动线程?-收到这张表格您上面复制的代码

您能提供更完整的代码吗?这段代码是如何调用的?当线程到达run()函数的末尾时,它应该自行退出。您确定调用线程时没有出现错误吗?例如,您是否多次调用此代码?