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
If I use a desktop link:
#!/usr/bin/env xdg-open
[Desktop Entry]
Type=Application
Name=MyProblem
Exec=/home/USER/myProblem.py
Terminal=false
Name[en_CA]=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).
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
Since this is a threading.Timer, there does not appear to be a way of using daemon=True.
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