Python 计时器线程不使用桌面快捷方式终止 [英] Python Timer Threading not Terminating using Desktop Shortcut

查看:108
本文介绍了Python 计时器线程不使用桌面快捷方式终止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我从终端运行以下代码并退出窗口后,使用 Ctrl+C 或 Ctrl+Z 取消脚本时,脚本完全终止.使用 Ctrl+C 我得到这个错误:

When I run the following code from the terminal and after exiting the window, use either Ctrl+C or Ctrl+Z to cancel the script, the script COMPLETELY terminates. With Ctrl+C I get this error:

Exception ignored in: <module 'threading' from '/usr/lib/python3.6/threading.py'>
Traceback (most recent call last):
  File "/usr/lib/python3.6/threading.py", line 1294, in _shutdown
    t.join()
  File "/usr/lib/python3.6/threading.py", line 1056, in join
    self._wait_for_tstate_lock()
  File "/usr/lib/python3.6/threading.py", line 1072, in _wait_for_tstate_lock
    elif lock.acquire(block, timeout):
KeyboardInterrupt

如果我使用桌面链接:

#!/usr/bin/env xdg-open

[Desktop Entry]
Type=Application
Name=MyProblem
Exec=/home/USER/myProblem.py
Terminal=false
Name[en_CA]=myProblem

并退出窗口,线程似乎仍在运行(通过尝试注销我的桌面并出现一个通知程序,表明 MyProblem 正忙).

and exit the window, it appears that the thread is still running (indicated by trying to log off my desktop and a notifier appearing saying that MyProblem is busy).

线程代码没有终止,据我所知,在等待期后触发线程后,很难终止.我看了很多帖子,包括:Python threading.timer - 每 'n' 秒重复一次函数Python线程,线程不关闭

The threading code is not terminating and I understand that after threading has been triggered after the waiting period, it is difficult to terminate. I'v looked at numerous posts including: Python threading.timer - repeat function every 'n' seconds Python threading, threads do not close

由于这是一个 threading.Timer,似乎没有使用 daemon=True 的方法.

Since this is a threading.Timer, there does not appear to be a way of using daemon=True.

我不确定如何在离开窗口之前在 closeEvent 中实现线程的完全关闭.每个人都保持安全.谢谢.

I am not sure how to implement a complete shutdown of threading in the closeEvent before leaving the window. Everyone stay safe. Thank you.

#!/usr/bin/python3
from datetime import datetime, timedelta
from PyQt5.QtWidgets import (QAction, QApplication, QCheckBox, QGridLayout, QMainWindow, QMenuBar, QWidget)
import os; import subprocess; import sys; import threading; import time
class MyProblem(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()
########################################
    def closeEvent(self, event):
# what do I place here - thr is a NoneType
        None
########################################
    def recordTimedAudio(self, tL, tfn):
# record audio
        self.rec_args =['arecord', '--device=hw', '-d', str(tL), '-f', 'cd', '-t', 'wav', tfn +'.wav']
        self.rec = subprocess.Popen(self.rec_args, shell=False)
########################################
    def timedRecording(self):
        dateData =['2020-04-13 13:06:00','2020-04-13 13:07:00'] # this should be changed for future dates/times
# My code uses a for loop to create a number of threads, but the end go through a series
        for d in dateData:
# Create Start Time in Seconds
            t = time.strptime(d, '%Y-%m-%d %H:%M:%S')
            t = time.mktime(t)
            thr = threading.Timer(int(t-time.time()), self.recordTimedAudio, ['10','File1']).start() #/27190809/
            print(thr) # This comes back None
            self.timerAction.setEnabled(False)
########################################
    def initUI(self):
        self.setGeometry(5,5,100,100); self.mainWidget = QWidget()
        self.layout = QGridLayout();   self.mainWidget.setLayout(self.layout)
        self.setCentralWidget(self.mainWidget)
# Menu
        self.mainMenu = self.menuBar()
        self.optionsMenu = self.mainMenu.addMenu('Options')
        self.timerAction = QAction('Set Timer', checkable=True)
        self.timerAction.triggered.connect(self.timedRecording)
        self.optionsMenu.addAction(self.timerAction)
        self.show()
########################################
if __name__ =='__main__':
    app = QApplication(sys.argv)
    ex = MyProblem()
    sys.exit(app.exec_())
################################################################################################END

推荐答案

我无法停止线程,也许线程对于这样的计时问题来说太过分了.我创建了一个解决方法,可能会在未来帮助某人.用这些例程替换上面的代码:

I couldn't get the thread to stop and maybe threading was overkill for such a timing problem. I created a workaround that may help someone in the future. Replace the above code with these routines:

def timedRecording(self):
    dateData =['2020-04-13 13:06:00','2020-04-13 13:07:00'] # this should be changed for future dates/times
    for d in dateData:
# Create Start Time in Seconds
        t = time.strptime(d, '%Y-%m-%d %H:%M:%S')
        t = time.mktime(t)
        self.timerData.append([t, trecLength, urlText, tfilename]) # start time recording length, url, filename
    self.timerData.sort(key = lambda x: x[0]) # /4174941/
    self.treclen = len(self.timerData)
    if self.treclen>0:
        self.rtime.setText('Timer Set')
        self.timerAction.setEnabled(False)
        self.startTRecTimer()
########################################
def recordTimedAudio(self, tL, urlText, tfn):
    self.rec_args =['arecord', '--device=hw', '-d', str(tL), '-f', 'cd', '-t', 'wav', os.path.join(os.path.sep,self.audioDir, tfn +'.wav')]
    self.rec = subprocess.Popen(self.rec_args, shell=False) #https://docs.python.org/2/library/subprocess.html#popen-constructor
# Learning a trick from: https://stackoverflow.com/questions/27190809/
    if self.trecNum < self.treclen-1:
        self.trecNum+=1
        self.startTRecTimer()
########################################
def startTRecTimer(self):
    if time.time() < self.timerData[self.trecNum][0]: # time has not elapsed /415511/
# Start QTimer delay then Start Recording
        delayTime = int(self.timerData[self.trecNum][0]-time.time())*1000 # QTimer uses milliseconds
        self.trecTimer = QTimer()
        self.trecTimer.setSingleShot(True)
        self.trecTimer.timeout.connect(lambda: self.recordTimedAudio(self.timerData[self.trecNum][1], self.timerData[self.trecNum][2], self.timerData[self.trecNum][3]))
        self.trecTimer.start(delayTime)

还添加到 initUI:

Also add to initUI:

self.rec = None

窗口的closeEvent:

And to the closeEvent of the window:

        if self.rec != None:
            self.rec.terminate() # /16866602/

所以子进程停止了.

这篇关于Python 计时器线程不使用桌面快捷方式终止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆